示例#1
0
        public virtual async Task <OAuthV1Info> GetOAuthInfo(string redirectUri, string scope = null)
        {
            string token       = null;
            string tokenSecret = null;

            using (HttpClient client = CreateHttpClient())
            {
                client.DefaultRequestHeaders.Authorization = OAuthV1Utils.GetOAuthHeader(
                    _clientId,
                    _clientSecret,
                    HttpMethod.Post,
                    requestUri: OAuthRequestTokenUri,
                    redirectUri: redirectUri,
                    scope: scope);
                using (var response = await client.PostAsync(OAuthRequestTokenUri, null))
                {
                    string content = null;
                    if (response.Content != null)
                    {
                        content = await response.Content.ReadAsStringAsync();
                    }

                    if (!response.IsSuccessStatusCode)
                    {
                        // content is html (not json).  pass null explicitly
                        throw new OAuthException(String.Format("{0} GetOAuthInfo: {1}", Name, GetErrorMessage(null, response.StatusCode)), response.StatusCode, content);
                    }

                    var query = HttpUtility.ParseQueryString(content);

                    token = query["oauth_token"];
                    if (String.IsNullOrEmpty(token))
                    {
                        throw new OAuthException(String.Format("{0} GetOAuthInfo: Missing oauth_token.", Name), response.StatusCode, content);
                    }

                    tokenSecret = query["oauth_token_secret"];
                    if (String.IsNullOrEmpty(tokenSecret))
                    {
                        throw new OAuthException(String.Format("{0} GetOAuthInfo: Missing oauth_token_secret.", Name), response.StatusCode, content);
                    }
                }
            }

            StringBuilder strb = new StringBuilder();

            strb.Append(OAuthAuthorizeUri);
            strb.AppendFormat("?oauth_token={0}", WebUtility.UrlEncode(token));
            strb.AppendFormat("&oauth_callback={0}", WebUtility.UrlEncode(redirectUri));

            return(new OAuthV1Info(strb.ToString(), token, tokenSecret));
        }
示例#2
0
 public virtual async Task <bool> PatchAsJsonAsync <T>(string operation, string requestUri, string token, string tokenSecret, T value)
 {
     using (HttpClient client = CreateHttpClient())
     {
         client.DefaultRequestHeaders.Authorization = OAuthV1Utils.GetOAuthHeader(
             _clientId,
             _clientSecret,
             new HttpMethod("PATCH"),
             requestUri,
             token: token,
             tokenSecret: tokenSecret);
         using (var response = await client.PatchAsJsonAsync(requestUri, value))
         {
             return(await ProcessEmptyResponse(operation, response));
         }
     }
 }
示例#3
0
 public virtual async Task <T> GetAsync <T>(string operation, string requestUri, string token, string tokenSecret)
 {
     using (HttpClient client = CreateHttpClient())
     {
         client.DefaultRequestHeaders.Authorization = OAuthV1Utils.GetOAuthHeader(
             _clientId,
             _clientSecret,
             HttpMethod.Get,
             requestUri,
             token: token,
             tokenSecret: tokenSecret);
         using (var response = await client.GetAsync(requestUri))
         {
             return(await ProcessResponse <T>(operation, response));
         }
     }
 }
示例#4
0
        public virtual async Task <OAuthV1Info> Authorize(string verifier, string token, string tokenSecret)
        {
            using (HttpClient client = CreateHttpClient())
            {
                client.DefaultRequestHeaders.Authorization = OAuthV1Utils.GetOAuthHeader(
                    _clientId,
                    _clientSecret,
                    HttpMethod.Post,
                    requestUri: OAuthAccessTokenUri,
                    token: token,
                    tokenSecret: tokenSecret,
                    verifier: verifier);
                using (var response = await client.PostAsync(OAuthAccessTokenUri, null))
                {
                    string content = null;
                    if (response.Content != null)
                    {
                        content = await response.Content.ReadAsStringAsync();
                    }

                    if (!response.IsSuccessStatusCode)
                    {
                        // content is html (not json).  pass null explicitly
                        throw new OAuthException(String.Format("{0} Authorize: {1}", Name, GetErrorMessage(null, response.StatusCode)), response.StatusCode, content);
                    }

                    var query = HttpUtility.ParseQueryString(content);

                    token = query["oauth_token"];
                    if (String.IsNullOrEmpty(token))
                    {
                        throw new OAuthException(String.Format("{0} Authorize: Missing oauth_token.", Name), response.StatusCode, content);
                    }

                    tokenSecret = query["oauth_token_secret"];
                    if (String.IsNullOrEmpty(tokenSecret))
                    {
                        throw new OAuthException(String.Format("{0} Authorize: Missing oauth_token_secret.", Name), response.StatusCode, content);
                    }

                    return(new OAuthV1Info(null, token, tokenSecret));
                }
            }
        }
示例#5
0
        public virtual async Task <StreamContent> GetStreamAsync(string operation, string requestUri, string token, string tokenSecret)
        {
            using (HttpClient client = CreateHttpClient())
            {
                client.DefaultRequestHeaders.Authorization = OAuthV1Utils.GetOAuthHeader(
                    _clientId,
                    _clientSecret,
                    HttpMethod.Get,
                    requestUri,
                    token: token,
                    tokenSecret: tokenSecret);

                var response = await client.GetAsync(requestUri);

                if (response.IsSuccessStatusCode)
                {
                    return((StreamContent)response.Content);
                }

                throw new OAuthException(String.Format("{0} {1}: {2}", Name, operation, GetErrorMessage(null, response.StatusCode)), response.StatusCode, String.Empty);
            }
        }