コード例 #1
0
        public HttpWebResponse GetResponse(OAuthContext context, TokenBase accessToken)
        {
            SignContext(context, accessToken);

            Uri uri = context.GenerateUri();

            Console.WriteLine("Uri: {0}", uri);

            var request = (HttpWebRequest) WebRequest.Create(uri);
            request.Method = context.RequestMethod;

            if ((context.FormEncodedParameters != null) && (context.FormEncodedParameters.Count > 0))
            {
                request.ContentType = "application/x-www-form-urlencoded";
                using (var writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(UriUtility.FormatQueryString(context.FormEncodedParameters));
                }
            }

            if (UseHeaderForOAuthParameters)
            {
                request.Headers[Parameters.OAuth_Authorization_Header] = context.GenerateOAuthParametersForHeader();
            }

            return (HttpWebResponse) request.GetResponse();
        }
コード例 #2
0
        public void MakeAuthenticatedCallForTokenRsaSha1WithPost()
        {
            var accessToken = new TokenBase {ConsumerKey = "key", Token = "accesskey", TokenSecret = "accesssecret"};
            OAuthConsumer consumer = CreateConsumer(SignatureMethod.RsaSha1);

            var factory = new OAuthContextFactory();
            OAuthContext context = factory.FromUri("POST", new Uri("http://term.ie/oauth/example/echo_api.php"));
            context.FormEncodedParameters.Add("success", "true");

            HttpWebResponse response = consumer.GetResponse(context, accessToken);

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                string contents = reader.ReadToEnd();
                Assert.AreEqual("success=true", contents);
            }
        }
コード例 #3
0
        public TokenBase ExchangeRequestTokenForAccessToken(TokenBase requestToken,
                                                            NameValueCollection additionalQueryParameters)
        {
            OAuthContext context = BuildExchangeRequestTokenForAccessTokenContext(requestToken,
                                                                                  additionalQueryParameters);

            TokenBase accessToken = context.InvokeHttpWebRequest(collection =>
                                                                 new TokenBase
                                                                     {
                                                                         ConsumerKey = requestToken.ConsumerKey,
                                                                         Token =
                                                                             ParseResponseParameter(collection,
                                                                                                    Parameters.
                                                                                                        OAuth_Token),
                                                                         TokenSecret =
                                                                             ParseResponseParameter(collection,
                                                                                                    Parameters.
                                                                                                        OAuth_Token_Secret)
                                                                     });

            return accessToken;
        }
コード例 #4
0
        public void SignContext(OAuthContext context, TokenBase accessToken)
        {
            EnsureStateIsValid();

            if (accessToken.ConsumerKey != ConsumerKey)
                throw Error.SuppliedTokenWasNotIssuedToThisConsumer(ConsumerKey, accessToken.ConsumerKey);

            var signer = new OAuthContextSigner();
            var auth = new NonceGenerator();

            context.UseAuthorizationHeader = UseHeaderForOAuthParameters;
            context.ConsumerKey = accessToken.ConsumerKey;
            context.Token = accessToken.Token;
            context.TokenSecret = accessToken.TokenSecret;
            context.SignatureMethod = SignatureMethod;
            context.Timestamp = DateTime.Now.EpocString();
            context.Nonce = auth.GenerateNonce();
            context.Version = "1.0";

            string signatureBase = context.GenerateSignatureBase();

            Console.WriteLine("signature_base: {0}", signatureBase);

            signer.SignContext(context,
                               new SigningContext
                                   {Algorithm = Key, SignatureBase = signatureBase, ConsumerSecret = ConsumerSecret});

            Console.WriteLine("oauth_singature: {0}", context.Signature);
        }
コード例 #5
0
        public OAuthContext BuildExchangeRequestTokenForAccessTokenContext(TokenBase requestToken,
                                                                           NameValueCollection additionalQueryParameters)
        {
            EnsureStateIsValid();

            if (requestToken.ConsumerKey != ConsumerKey)
                throw Error.SuppliedTokenWasNotIssuedToThisConsumer(ConsumerKey, requestToken.ConsumerKey);

            var auth = new NonceGenerator();

            var factory = new OAuthContextFactory();
            var signer = new OAuthContextSigner();
            OAuthContext context = factory.FromUri("GET", AccessTokenUri);

            if (additionalQueryParameters != null)
                context.QueryParameters.Add(additionalQueryParameters);

            context.ConsumerKey = ConsumerKey;
            context.Token = requestToken.Token;
            context.TokenSecret = requestToken.TokenSecret;
            context.RequestMethod = "GET";
            context.SignatureMethod = SignatureMethod;
            context.Timestamp = DateTime.Now.EpocString();
            context.Nonce = auth.GenerateNonce();
            context.Version = "1.0";

            string signatureBase = context.GenerateSignatureBase();

            Console.WriteLine("signature_base: {0}", signatureBase);

            signer.SignContext(context,
                               new SigningContext
                                   {Algorithm = Key, SignatureBase = signatureBase, ConsumerSecret = ConsumerSecret});

            Console.WriteLine("oauth_singature: {0}", context.Signature);

            Uri uri = context.GenerateUri();

            Console.WriteLine("Uri: {0}", uri);

            return context;
        }