Exemple #1
0
        public TokenManager(TokenOptions options)
        {
            _iamUrl    = !string.IsNullOrEmpty(options.IamUrl) ? options.IamUrl : "https://iam.bluemix.net/identity/token";
            _tokenInfo = new IamTokenData();
            if (!string.IsNullOrEmpty(options.IamApiKey))
            {
                _iamApikey = options.IamApiKey;
            }
            if (!string.IsNullOrEmpty(options.IamAccessToken))
            {
                _userAccessToken = options.IamAccessToken;
            }

            this.Client = new WatsonHttpClient(this._iamUrl);
        }
Exemple #2
0
        /// <summary>
        /// Request an IAM token using an API key.
        /// </summary>
        /// <returns>An IamTokenData object containing the IAM token.</returns>
        private IamTokenData RequestToken()
        {
            IamTokenData result = null;

            try
            {
                if (string.IsNullOrEmpty(_iamApikey))
                {
                    throw new ArgumentNullException(nameof(_iamApikey));
                }

                var request = this.Client.PostAsync(_iamUrl);
                request.WithHeader("Content-type", "application/x-www-form-urlencoded");
                request.WithHeader("Authorization", "Basic Yng6Yng=");

                List <KeyValuePair <string, string> > content      = new List <KeyValuePair <string, string> >();
                KeyValuePair <string, string>         grantType    = new KeyValuePair <string, string>("grant_type", "urn:ibm:params:oauth:grant-type:apikey");
                KeyValuePair <string, string>         responseType = new KeyValuePair <string, string>("response_type", "cloud_iam");
                KeyValuePair <string, string>         apikey       = new KeyValuePair <string, string>("apikey", _iamApikey);
                content.Add(grantType);
                content.Add(responseType);
                content.Add(apikey);

                var formData = new FormUrlEncodedContent(content);

                request.WithBodyContent(formData);

                result = request.As <IamTokenData>().Result;

                if (result == null)
                {
                    result = new IamTokenData();
                }
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }
Exemple #3
0
 /// <summary>
 /// Save the response from the IAM service request to the object's state.
 /// </summary>
 /// <param name="tokenResponse"></param>
 private void SaveTokenInfo(IamTokenData tokenResponse)
 {
     _tokenInfo = tokenResponse;
 }