示例#1
0
        public YubiCryptClient(string clientId, string clientSecret, YubiCryptOAuth2Token clientToken)
        {
            InitHttpClient();

            _clientId     = clientId;
            _clientSecret = clientSecret;
            OAuth2Token   = clientToken;
        }
示例#2
0
        public async Task <YubiCryptOAuth2Token> RefreshToken(string refreshToken)
        {
            _httpClient.DefaultRequestHeaders.Authorization = null;

            var request = new HttpRequestMessage(HttpMethod.Post, "/OAuth/Token");

            var parameters = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("grant_type", "refresh_token"),
                new KeyValuePair <string, string>("refresh_token", refreshToken),
                new KeyValuePair <string, string>("client_id", _clientId),
                new KeyValuePair <string, string>("client_secret", _clientSecret)
            };

            request.Content = new FormUrlEncodedContent(parameters);

            var response = await _httpClient.SendAsync(request);

            var resposeContent = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception(string.Format("An HTTP Error was returned: {0} {1}", response.StatusCode, response.ReasonPhrase));
            }


            var token = JsonConvert.DeserializeObject <YubiCryptOAuth2TokenResponse>(resposeContent);

            var resultToken = new YubiCryptOAuth2Token()
            {
                AccessToken  = token.access_token,
                Expires      = DateTime.Now.AddSeconds(token.expires_in),
                RefreshToken = token.refresh_token,
                TokenType    = token.token_type
            };

            return(resultToken);
        }