Esempio n. 1
0
        public void RequestForAccessToken()
        {
            QueryParameters oauthParameters = new QueryParameters();
            string          signature       = "";
            OAuthHelper     oauthHelper     = new OAuthHelper();

            ////1. Generate Signature
            oauthParameters.Add("oauth_consumer_key", provider.Consumerkey);
            oauthParameters.Add("oauth_token", ConnectionToken.AuthorizationToken);
            oauthParameters.Add("oauth_signature_method", provider.SignatureMethod.ToString());
            oauthParameters.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp());
            oauthParameters.Add("oauth_nonce", oauthHelper.GenerateNonce());
            oauthParameters.Add("oauth_version", "1.0");
            oauthParameters.Add("oauth_verifier", ConnectionToken.OauthVerifier);
            signature = oauthHelper.GenerateSignature(new Uri(provider.AccessTokenEndpoint), oauthParameters, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, provider.TransportName, ConnectionToken.TokenSecret);
            oauthParameters.Add("oauth_signature", signature);

            //2. Notify Consumer (if applicable)
            BeforeRequestingAccessToken(oauthParameters); // hook called

            //3.Connect and obtain Token
            string targetUrl = provider.AccessTokenEndpoint + "?" +
                               oauthHelper.GetAuthorizationUrlParameters(oauthParameters);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);

            request.Method = provider.TransportName.ToString();
            //request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParameters));
            request.ContentLength = 0;
            string response = "";

            try
            {
                logger.Debug("Requesting Access Token at " + provider.AccessTokenEndpoint);
                using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
                    using (Stream responseStream = webResponse.GetResponseStream())
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            response = reader.ReadToEnd();
                            var responseCollection = Utility.GetQuerystringParameters(response);
                            HandleAccessTokenResponse(responseCollection);
                            AfterGettingAccessToken(responseCollection, ConnectionToken);
                        }
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.AccessTokenRequestError(provider.AccessTokenEndpoint, oauthParameters), ex);
                throw new OAuthException(ErrorMessages.AccessTokenRequestError(provider.AccessTokenEndpoint, oauthParameters), ex);
            }
        }
Esempio n. 2
0
        public void RequestForRequestToken()
        {
            QueryParameters oauthParameters = new QueryParameters();
            string          signature       = "";
            OAuthHelper     oauthHelper     = new OAuthHelper();


            //Twitter Test @ https://dev.twitter.com/docs/auth/oauth
            //oauthParameters.Add("oauth_callback", "http://localhost:3005/the_dance/process_callback?service_provider_id=11");
            //oauthParameters.Add("oauth_consumer_key", "GDdmIQH6jhtmLUypg82g");
            //oauthParameters.Add("oauth_nonce", "QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk");
            //oauthParameters.Add("oauth_signature_method", "HMAC-SHA1");
            //oauthParameters.Add("oauth_timestamp", "1272323042");
            //oauthParameters.Add("oauth_version", "1.0");
            //signature = oauthHelper.GenerateSignature(new Uri(provider.RequestTokenEndpoint), oauthParameters, "GDdmIQH6jhtmLUypg82g", "MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98", provider.SignatureMethod, provider.TransportName, string.Empty);

            ////1. Setup request parameters
            oauthParameters.Add("oauth_consumer_key", provider.Consumerkey);
            oauthParameters.Add("oauth_signature_method", provider.SignatureMethod.ToString());
            oauthParameters.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp());
            oauthParameters.Add("oauth_nonce", oauthHelper.GenerateNonce());
            oauthParameters.Add("oauth_version", "1.0");
            oauthParameters.Add("oauth_callback", ConnectionToken.Domain + "");

            //2. Notify Consumer (optionally user may wish to add extra parameters)
            BeforeRequestingRequestToken(oauthParameters); // hook called

            oauthParameters.ToList().ForEach(x => x.Value = Utility.UrlEncode(x.Value));

            //3. Generate Signature
            signature = oauthHelper.GenerateSignature(new Uri(provider.RequestTokenEndpoint), oauthParameters, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, provider.TransportName, string.Empty);
            oauthParameters.Add("oauth_signature", signature);


            //4.Connect and obtain Token
            logger.Debug("Requesting Request Token at: " + provider.RequestTokenEndpoint);
            string         requestUrl = provider.RequestTokenEndpoint + "?" + oauthHelper.GetAuthorizationUrlParameters(oauthParameters);
            HttpWebRequest request    = (HttpWebRequest)HttpWebRequest.Create(requestUrl);

            request.Method = provider.TransportName.ToString();
            //request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParameters));
            request.ContentLength = 0;
            //request.ContentType = "application/x-www-form-urlencoded";
            //TODO: Check issue with Authorization Header
            string response = "";

            try
            {
                logger.Debug("Requesting Request Token at: " + provider.RequestTokenEndpoint);
                using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
                    using (Stream responseStream = webResponse.GetResponseStream())
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            response = reader.ReadToEnd();
                            if (response.Contains("oauth_token_secret"))
                            {
                                logger.Debug("Request Token response: " + response.ToString());
                                var responseCollection = Utility.GetQuerystringParameters(response);
                                HandleRequestTokenGrant(responseCollection);
                            }
                        }
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.RequestTokenRequestError(provider.RequestTokenEndpoint, oauthParameters), ex);
                throw new OAuthException(ErrorMessages.RequestTokenRequestError(provider.RequestTokenEndpoint, oauthParameters), ex);
            }
        }
        public void RequestForRequestToken()
        {

            QueryParameters oauthParameters = new QueryParameters();
            string signature = "";
            OAuthHelper oauthHelper = new OAuthHelper();


            //Twitter Test @ https://dev.twitter.com/docs/auth/oauth
            //oauthParameters.Add("oauth_callback", "http://localhost:3005/the_dance/process_callback?service_provider_id=11");
            //oauthParameters.Add("oauth_consumer_key", "GDdmIQH6jhtmLUypg82g");
            //oauthParameters.Add("oauth_nonce", "QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk");
            //oauthParameters.Add("oauth_signature_method", "HMAC-SHA1");
            //oauthParameters.Add("oauth_timestamp", "1272323042");
            //oauthParameters.Add("oauth_version", "1.0");
            //signature = oauthHelper.GenerateSignature(new Uri(provider.RequestTokenEndpoint), oauthParameters, "GDdmIQH6jhtmLUypg82g", "MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98", provider.SignatureMethod, provider.TransportName, string.Empty);

            ////1. Setup request parameters
            oauthParameters.Add("oauth_consumer_key", provider.Consumerkey);
            oauthParameters.Add("oauth_signature_method", provider.SignatureMethod.ToString());
            oauthParameters.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp());
            oauthParameters.Add("oauth_nonce", oauthHelper.GenerateNonce());
            oauthParameters.Add("oauth_version", "1.0");
            oauthParameters.Add("oauth_callback", connectionToken.Domain + "SocialAuth/validate.sauth");

            //2. Notify Consumer (optionally user may wish to add extra parameters)
            BeforeRequestingRequestToken(oauthParameters); // hook called

            //3. Generate Signature
            signature = oauthHelper.GenerateSignature(new Uri(provider.RequestTokenEndpoint), oauthParameters, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, provider.TransportName, string.Empty);
            oauthParameters.Add("oauth_signature", signature);


            //4.Connect and obtain Token
            logger.Debug("Requesting Request Token at: " + provider.RequestTokenEndpoint);
            string requestUrl = provider.RequestTokenEndpoint + "?" + oauthHelper.GetAuthorizationUrlParameters(oauthParameters);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
            request.Method = provider.TransportName.ToString();
            //request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParameters));
            request.ContentLength = 0;
            //request.ContentType = "application/x-www-form-urlencoded";
            //TODO: Check issue with Authorization Header
            string response = "";

            try
            {

                logger.Debug("Requesting Request Token at: " + provider.RequestTokenEndpoint);
                using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
                using (Stream responseStream = webResponse.GetResponseStream())
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    response = reader.ReadToEnd();
                    if (response.Contains("oauth_token_secret"))
                    {
                        logger.Debug("Request Token response: " + response.ToString());
                        var responseCollection = Utility.GetQuerystringParameters(response);
                        HandleRequestTokenGrant(responseCollection);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.RequestTokenRequestError(provider.RequestTokenEndpoint, oauthParameters), ex);
                throw new OAuthException(ErrorMessages.RequestTokenRequestError(provider.RequestTokenEndpoint, oauthParameters), ex);
            }
        }
        public void RequestForAccessToken()
        {
            QueryParameters oauthParameters = new QueryParameters();
            string signature = "";
            OAuthHelper oauthHelper = new OAuthHelper();

            ////1. Generate Signature
            oauthParameters.Add("oauth_consumer_key", provider.Consumerkey);
            oauthParameters.Add("oauth_token", connectionToken.AuthorizationToken);
            oauthParameters.Add("oauth_signature_method", provider.SignatureMethod.ToString());
            oauthParameters.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp());
            oauthParameters.Add("oauth_nonce", oauthHelper.GenerateNonce());
            oauthParameters.Add("oauth_version", "1.0");
            oauthParameters.Add("oauth_verifier", connectionToken.OauthVerifier);
            signature = oauthHelper.GenerateSignature(new Uri(provider.AccessTokenEndpoint), oauthParameters, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, provider.TransportName, connectionToken.TokenSecret);
            oauthParameters.Add("oauth_signature", signature);

            //2. Notify Consumer (if applicable)
            BeforeRequestingAccessToken(oauthParameters); // hook called

            //3.Connect and obtain Token
            string targetUrl = provider.AccessTokenEndpoint + "?" +
                               oauthHelper.GetAuthorizationUrlParameters(oauthParameters);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);
            request.Method = provider.TransportName.ToString();
            //request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParameters));
            request.ContentLength = 0;
            string response = "";

            try
            {
                logger.Debug("Requesting Access Token at " + provider.AccessTokenEndpoint);
                using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
                using (Stream responseStream = webResponse.GetResponseStream())
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    response = reader.ReadToEnd();
                    var responseCollection = Utility.GetQuerystringParameters(response);
                    HandleAccessTokenResponse(responseCollection);

                }

            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.AccessTokenRequestError(provider.AccessTokenEndpoint, oauthParameters), ex);
                throw new OAuthException(ErrorMessages.AccessTokenRequestError(provider.AccessTokenEndpoint, oauthParameters), ex);
            }


        }