private IEnumerable GetOAuthCredentials()

in Mail2Bug/WorkItemManagement/TFSWorkItemManager.cs [135:163]


        private IEnumerable<TfsClientCredentials> GetOAuthCredentials()
        {
            try
            {
                var usernameAndPassword = GetServiceIdentityUsernameAndPasswordFromConfig();

                if (usernameAndPassword == null || 
                    string.IsNullOrEmpty(_config.TfsServerConfig.OAuthClientId) ||
                    string.IsNullOrEmpty(_config.TfsServerConfig.OAuthContext) ||
                    string.IsNullOrEmpty(_config.TfsServerConfig.OAuthResourceId))
                {
                    return new List<TfsClientCredentials>();
                }

                var userCredential = new UserCredential(usernameAndPassword.Item1, usernameAndPassword.Item2);
                var authContext = new AuthenticationContext(_config.TfsServerConfig.OAuthContext);
                var result = authContext.AcquireToken(_config.TfsServerConfig.OAuthResourceId, _config.TfsServerConfig.OAuthClientId, userCredential);
                var oauthToken = new OAuthTokenCredential(result.AccessToken);
                return new List<TfsClientCredentials>()
                {
                    new TfsClientCredentials(oauthToken)
                };
            }
            catch (Exception ex)
            {
                Logger.WarnFormat("Error trying to generate OAuth Token for TFS connection\n{0}", ex);
                return new List<TfsClientCredentials>();
            }
        }