コード例 #1
0
        public static OAuthTicket AuthorizeWithCredentials(OAuthTicket ticket, string username, string password, string baseUrl, string authorizeUrl)
        {
            var restClient = new RestSharp.RestClient(baseUrl);

            restClient.Authenticator = OAuth1Authenticator.ForClientAuthentication(ticket.ConsumerKey, ticket.ConsumerSecret, username, password);

            var request = new RestRequest(authorizeUrl, Method.POST);

            byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(username + " " + password);

            request.AddHeader("Content-Type", "application/xml");
            request.AddParameter("ec", System.Convert.ToBase64String(toEncodeAsBytes, 0, toEncodeAsBytes.Length));
            var response = restClient.Execute(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new ApiAccessException(response.StatusDescription)
                      {
                          StatusCode        = response.StatusCode,
                          StatusDescription = response.StatusDescription,
                          RequestUrl        = response.ResponseUri.AbsoluteUri
                      };
            }
            else
            {
                var qs = HttpUtility.ParseQueryString(response.Content);
                ticket.AccessToken       = qs["oauth_token"];
                ticket.AccessTokenSecret = qs["oauth_token_secret"];
                return(ticket);
            }
        }
コード例 #2
0
        public virtual IRestResponse AuthorizeFirstParty(OAuthTicket ticket, string username, string password, string authorizeUrl)
        {
            var restClient = new RestSharp.RestClient(this.BaseUrl);

            restClient.Authenticator = OAuth1Authenticator.ForClientAuthentication(ticket.ConsumerKey, ticket.ConsumerSecret, username, password);

            var request = new RestRequest(authorizeUrl, Method.POST);

            byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(username + " " + password);

            request.AddHeader("Content-Type", "application/xml");
            request.AddParameter("ec", System.Convert.ToBase64String(toEncodeAsBytes, 0, toEncodeAsBytes.Length));
            var response = restClient.Execute(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new ApiAccessException(response.StatusDescription)
                      {
                          StatusCode        = response.StatusCode,
                          StatusDescription = response.StatusDescription,
                          RequestUrl        = response.ResponseUri.AbsoluteUri
                      };
            }
            else
            {
                return(response);
            }
        }