public async Task <TokenResponse <AccessToken> > GetAccessTokenAsync(string accessTokenUrl, AuthToken authToken, string grantType, IEnumerable <KeyValuePair <string, string> > parameters = null, HttpContent postValue = null) { Precondition.NotNull(accessTokenUrl, "accessTokenUrl"); Precondition.NotNull(authToken, "authToken"); Precondition.NotNull(grantType, "grantType"); Precondition.NotNull(_clientId, "clientId"); Precondition.NotNull(_clientSecret, "clientSecret"); var sendParameters = new List <KeyValuePair <string, string> >(8) { new KeyValuePair <string, string>(OAuthConstants.CODE, authToken.Code), new KeyValuePair <string, string>(OAuthConstants.CLIENT_SECRET, _clientSecret), new KeyValuePair <string, string>(OAuthConstants.REDIRECT_URI, _redirectUrl), new KeyValuePair <string, string>(OAuthConstants.GRANT_TYPE, grantType), new KeyValuePair <string, string>(OAuthConstants.SCOPE, _scope) }; if (parameters == null) { parameters = Enumerable.Empty <KeyValuePair <string, string> >(); } var handler = new OAuthMessageHandler(_clientId, _redirectUrl, parameters.Concat(sendParameters)); return(await GetTokenResponseAsync(accessTokenUrl, handler, postValue, (code, data) => new AccessToken(code, data))); }
public async Task <TokenResponse <AccessToken> > GetAccessTokenAsync(string accessTokenUrl, IEnumerable <KeyValuePair <string, string> > parameters = null, HttpContent postValue = null) { Precondition.NotNull(accessTokenUrl, "accessTokenUrl"); if (parameters == null) { parameters = Enumerable.Empty <KeyValuePair <string, string> >(); } var handler = new OAuthMessageHandler(_clientId, _redirectUrl, parameters); return(await GetTokenResponseAsync(accessTokenUrl, handler, postValue, (code, data) => new AccessToken(code, data))); }
private async Task <TokenResponse <T> > GetTokenResponseAsync <T>(string url, OAuthMessageHandler handler, HttpContent postValue, Func <string, ILookup <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> >())); var tokenBase = await response.Content.ReadAsStringAsync(); if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw new HttpRequestException(response.StatusCode + ":" + tokenBase); // error message } if (!tokenBase.Contains(OAuthConstants.ACCESS_TOKEN)) { return(null); } return(tokenBase.IsJson() ? ExtractTokenAndExtraDataJson(OAuthConstants.ACCESS_TOKEN, tokenFactory, tokenBase) : ExtractTokenAndExtraData(OAuthConstants.ACCESS_TOKEN, tokenFactory, tokenBase)); }