Esempio n. 1
0
        /// <summary>
        /// Allows OAuth applications to directly exchange Twitter usernames and passwords for OAuth access tokens and secrets.
        /// </summary>
        /// <param name="consumerKey">The consumer key.</param>
        /// <param name="consumerSecret">The consumer secret.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns>A <see cref="OAuthTokenResponse"/> instance.</returns>
        public static OAuthTokenResponse GetAccessTokens(string consumerKey, string consumerSecret, string username, string password)
        {
            if (string.IsNullOrEmpty(consumerKey))
            {
                throw new ArgumentNullException("consumerKey");
            }

            if (string.IsNullOrEmpty(consumerSecret))
            {
                throw new ArgumentNullException("consumerSecret");
            }

            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException("username");
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }

            OAuthTokenResponse response = new OAuthTokenResponse();

            try
            {
                WebRequestBuilder builder = new WebRequestBuilder(
                    new Uri("https://api.twitter.com/oauth/access_token"),
                    HTTPVerb.POST,
                    new OAuthTokens() { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret },
                    "");

                builder.Parameters.Add("x_auth_username", username);
                builder.Parameters.Add("x_auth_password", password);
                builder.Parameters.Add("x_auth_mode", "client_auth");

                string responseBody = new StreamReader(builder.ExecuteRequest().GetResponseStream()).ReadToEnd();

                response.Token = Regex.Match(responseBody, @"oauth_token=([^&]+)").Groups[1].Value;
                response.TokenSecret = Regex.Match(responseBody, @"oauth_token_secret=([^&]+)").Groups[1].Value;
                if (responseBody.Contains("user_id="))
                    response.UserId = long.Parse(Regex.Match(responseBody, @"user_id=([^&]+)").Groups[1].Value, CultureInfo.CurrentCulture);
                response.ScreenName = Regex.Match(responseBody, @"screen_name=([^&]+)").Groups[1].Value;
            }
            catch (WebException wex)
            {
                throw new Exception(wex.Message, wex);
            }

            return response;
        }
        public bool UserAccepted(string queryString)
        {
            NameValueCollection nvc = HttpUtility.ParseQueryString(queryString);
            string requestToken = nvc["oauth_token"];
            string verifier = nvc["oauth_verifier"];

            this.accessTokenResponse =
                OAuthUtility.GetAccessToken(this.ConsumerKey,
                                            this.ConsumerSecret,
                                            requestToken, string.Empty,
                                            verifier, LOLConstants.LinkTwitterAccessTokenUrl,
                                            HTTPVerb.GET, ServiceType.Twitter);

            this.UserId = Convert.ToString(this.accessTokenResponse.UserId);
            this.ScreenName = Convert.ToString(this.accessTokenResponse.ScreenName);
            return true;
        }
        public bool UserAccepted(string queryString)
        {
            NameValueCollection nvc = HttpUtility.ParseQueryString(queryString);
            string requestToken = nvc ["oauth_token"];
            string verifier = nvc ["oauth_verifier"];

            this.accessTokenResponse =
                OAuthUtility.GetAccessToken(this.ConsumerKey,
                                            this.ConsumerSecret,
                                            requestToken, this.authTokenResponse.TokenSecret,
                                            verifier, LOLConstants.LinkLinkedInAccessTokenUrl,
                                            HTTPVerb.POST, ServiceType.LinkedIn);

            return true;
        }
        public string GetBrowserAuthUrl()
        {
            this.authTokenResponse =
                OAuthUtility.GetRequestToken(LOLConstants.LinkedInConsumerKey,
                                             LOLConstants.LinkedInConsumerSecret,
                                             LOLConstants.LinkLinkedInCallbackUrl,
                                             LOLConstants.LinkLinkedInRequestTokenUrl, HTTPVerb.POST);

            Uri authorizationUri =
                OAuthUtility.BuildAuthorizationUri(authTokenResponse.Token, true, LOLConstants.LinkLinkedInAuthUrl);

            return authorizationUri.AbsoluteUri;
        }
Esempio n. 5
0
        public static OAuthTokenResponse GetAccessToken(string consumerKey, string consumerSecret, string requestToken, string accessTokenSecret, string verifier, string accessTokenUrl, HTTPVerb httpVerb, ServiceType serviceType)
        {
            if (string.IsNullOrEmpty(consumerKey))
            {
                throw new ArgumentNullException("consumerKey");
            }

            if (string.IsNullOrEmpty(consumerSecret))
            {
                throw new ArgumentNullException("consumerSecret");
            }

            if (string.IsNullOrEmpty(requestToken))
            {
                throw new ArgumentNullException("requestToken");
            }

            WebRequestBuilder builder = new WebRequestBuilder(
                new Uri(accessTokenUrl),
                httpVerb,
                //new OAuthTokens { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret },
                new OAuthTokens { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret, AccessToken = requestToken, AccessTokenSecret = accessTokenSecret },
                "");

            if (!string.IsNullOrEmpty(verifier))
            {
                builder.Parameters.Add("oauth_verifier", verifier);
            }

            builder.Parameters.Add("oauth_token", requestToken);

            string responseBody;

            try
            {
                HttpWebResponse webResponse = builder.ExecuteRequest();

                responseBody = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
            }
            catch (WebException wex)
            {
                throw new Exception(wex.Message, wex);
            }

            OAuthTokenResponse response = new OAuthTokenResponse();
            response.Token = Regex.Match(responseBody, @"oauth_token=([^&]+)").Groups[1].Value;
            response.TokenSecret = Regex.Match(responseBody, @"oauth_token_secret=([^&]+)").Groups[1].Value;

            if (serviceType == ServiceType.Twitter)
            {
                response.UserId = long.Parse(Regex.Match(responseBody, @"user_id=([^&]+)").Groups[1].Value, CultureInfo.CurrentCulture);
                response.ScreenName = Regex.Match(responseBody, @"screen_name=([^&]+)").Groups[1].Value;

            } else if (serviceType == ServiceType.DropBox)
            {

                response.UserId = long.Parse(Regex.Match(responseBody, @"uid=([^&]+)").Groups[1].Value, CultureInfo.CurrentCulture);

            }//end if
            return response;
        }