コード例 #1
0
        /// <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(consumerKey, consumerSecret, token: null, optionalOAuthHeaderParameters: parameters);

            return(GetTokenResponse(requestTokenUrl, handler, postValue, (key, secret) => new RequestToken(key, secret)));
        }
コード例 #2
0
        /// <summary>asynchronus get GetAccessToken</summary>
        public Task <TokenResponse <AccessToken> > GetAccessToken(string accessTokenUrl, RequestToken requestToken, IEnumerable <KeyValuePair <string, string> > parameters = null, HttpContent postValue = null)
        {
            Precondition.NotNull(accessTokenUrl, "accessTokenUrl");
            Precondition.NotNull(requestToken, "requestToken");

            if (parameters == null)
            {
                parameters = Enumerable.Empty <KeyValuePair <string, string> >();
            }
            var handler = new OAuthMessageHandler(consumerKey, consumerSecret, token: requestToken, optionalOAuthHeaderParameters: parameters);

            return(GetTokenResponse(accessTokenUrl, handler, postValue, (key, secret) => new AccessToken(key, secret)));
        }
コード例 #3
0
        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);

            response.EnsureSuccessStatusCode();

            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));
        }
コード例 #4
0
        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 != System.Net.HttpStatusCode.OK)
            {
                throw new HttpRequestException(response.StatusCode + ":" + tokenBase); // error message
            }

            var splitted  = tokenBase.Split('&').Select(s => s.Split('=')).ToDictionary(s => s.First(), s => s.Last());
            var token     = tokenFactory(splitted["oauth_token"].UrlDecode(), splitted["oauth_token_secret"].UrlDecode());
            var extraData = splitted.Where(kvp => kvp.Key != "oauth_token" && kvp.Key != "oauth_token_secret")
                            .ToLookup(kvp => kvp.Key, kvp => kvp.Value);

            return(new TokenResponse <T>(token, extraData));
        }