/// <summary>asynchronus get RequestToken</summary> public Task <TokenResponse <RequestToken> > GetRequestToken(string requestTokenUrl, IEnumerable <KeyValuePair <string, string> > parameters = null, HttpContent postValue = null) { Precondition.NotNull(requestTokenUrl, "requestTokenUrl"); var handler = new OAuthMessageHandler(Client, token: null, optionalOAuthHeaderParameters: parameters); return(GetTokenResponse(requestTokenUrl, handler, postValue, (key, secret) => new RequestToken(key, secret))); }
/// <summary>asynchronus get GetAccessToken</summary> public Task <TokenResponse <AccessToken> > GetAccessToken(string accessTokenUrl, RequestToken requestToken, string verifier, IEnumerable <KeyValuePair <string, string> > parameters = null, HttpContent postValue = null) { Precondition.NotNull(accessTokenUrl, "accessTokenUrl"); Precondition.NotNull(requestToken, "requestToken"); Precondition.NotNull(verifier, "verifier"); var verifierParam = new KeyValuePair <string, string>("oauth_verifier", verifier.Trim()); if (parameters == null) { parameters = Enumerable.Empty <KeyValuePair <string, string> >(); } var handler = new OAuthMessageHandler(Client, token: requestToken, optionalOAuthHeaderParameters: parameters.Concat(new[] { verifierParam })); return(GetTokenResponse(accessTokenUrl, handler, postValue, (key, secret) => new AccessToken(key, secret))); }
async Task <TokenResponse <T> > GetTokenResponse <T>(string url, OAuthMessageHandler handler, HttpContent postValue, Func <string, string, T> tokenFactory) where T : Token { var client = new HttpClient(handler); var response = await client.PostAsync(url, postValue ?? new FormUrlEncodedContent(Enumerable.Empty <KeyValuePair <string, string> >())).ConfigureAwait(false); var tokenBase = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (response.StatusCode != HttpStatusCode.OK) { throw new HttpRequestException(response.StatusCode + ":" + tokenBase); // error message } var splitted = tokenBase.Split('&').Select(s => s.Split('=')).ToLookup(xs => xs[0], xs => xs[1]); var token = tokenFactory(splitted["oauth_token"].First().UrlDecode(), splitted["oauth_token_secret"].First().UrlDecode()); var extraData = splitted.Where(kvp => kvp.Key != "oauth_token" && kvp.Key != "oauth_token_secret") .SelectMany(g => g, (g, value) => new { g.Key, Value = value }) .ToLookup(kvp => kvp.Key, kvp => kvp.Value); return(new TokenResponse <T>(token, extraData)); }