Ejemplo n.º 1
0
        public OAuth_Token OAuth_Convert_Request_To_Access_Token(OAuth_Token token)
        {
            HttpClient client = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage();

            request.RequestUri = new Uri("https://api.twitter.com/oauth/access_token");
            request.Method     = HttpMethod.Post;

            OAuthBase _oAuthBase = new OAuthBase();

            string normalizedUri;
            string normalizedParameters;
            string authHeader;

            string signature = _oAuthBase.GenerateSignature(
                request.RequestUri,
                null,
                oauthconsumerkey,
                oauthconsumersecret,
                token.oauth_token,
                null,
                request.Method.Method,
                _oAuthBase.GenerateTimeStamp(),
                _oAuthBase.GenerateNonce(),
                out normalizedUri,
                out normalizedParameters,
                out authHeader);

            request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", authHeader);
            request.Content = new StringContent("oauth_verifier=" + token.oauth_token_secret, Encoding.UTF8);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

            var response = client.SendAsync(request).Result;

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            string message                  = response.Content.ReadAsStringAsync().Result;
            NameValueCollection qs          = HttpUtility.ParseQueryString(message);
            OAuth_Token         oauth_token = new OAuth_Token();

            if (qs["oauth_token"] != null)
            {
                oauth_token.oauth_token = qs["oauth_token"];
            }
            if (qs["oauth_token_secret"] != null)
            {
                oauth_token.oauth_token_secret = qs["oauth_token_secret"];
            }

            return(oauth_token);
        }
Ejemplo n.º 2
0
        public ActionResult Callback(string oauth_token, string oauth_verifier)
        {
            //Verify that oauth_token == token recieved in Twitter Action.. Use a session variable?
            //if (!((OAuth_Token)Session["RequestToken"]).oauth_token.Equals(oauth_token))
     
            OAuth_Token token = new OAuth_Token { oauth_token = oauth_token, oauth_token_secret = oauth_verifier };

            //Step 3: Convert the request token to an access token
            TwitterService t = new TwitterService();
            OAuth_Token access_token  = t.OAuth_Convert_Request_To_Access_Token(token);

            //Store the access_token in users session for further use.
            Session.Add("Access_token", access_token);

            return RedirectToAction("Index");
        }
Ejemplo n.º 3
0
        public string GetSettings(OAuth_Token token)
        {
            HttpClient client = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage();

            request.RequestUri = new Uri("https://api.twitter.com/1.1/account/settings.json");
            request.Method     = HttpMethod.Get;

            OAuthBase _oAuthBase = new OAuthBase();

            string normalizedUri;
            string normalizedParameters;
            string authHeader;

            string signature = _oAuthBase.GenerateSignature(
                request.RequestUri,
                null,
                oauthconsumerkey,
                oauthconsumersecret,
                token.oauth_token,
                token.oauth_token_secret,
                request.Method.Method,
                _oAuthBase.GenerateTimeStamp(),
                _oAuthBase.GenerateNonce(),
                out normalizedUri,
                out normalizedParameters,
                out authHeader);

            request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", authHeader);

            var response = client.SendAsync(request).Result;

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            return(response.Content.ReadAsStringAsync().Result);
        }
Ejemplo n.º 4
0
        public string GetSettings(OAuth_Token token)
        {
            HttpClient client = new HttpClient();

                HttpRequestMessage request = new HttpRequestMessage();
                request.RequestUri = new Uri("https://api.twitter.com/1.1/account/settings.json");
                request.Method = HttpMethod.Get;

                OAuthBase _oAuthBase = new OAuthBase();

                string normalizedUri;
                string normalizedParameters;
                string authHeader;

                string signature = _oAuthBase.GenerateSignature(
                    request.RequestUri,
                    null,
                    oauthconsumerkey,
                    oauthconsumersecret,
                    token.oauth_token,
                    token.oauth_token_secret,
                    request.Method.Method,
                    _oAuthBase.GenerateTimeStamp(),
                    _oAuthBase.GenerateNonce(),
                    out normalizedUri,
                    out normalizedParameters,
                    out authHeader);

                request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", authHeader);

                var response = client.SendAsync(request).Result;
                if (!response.IsSuccessStatusCode)
                    return null;

               return response.Content.ReadAsStringAsync().Result;
        }
Ejemplo n.º 5
0
        public OAuth_Token OAuth_Request_Token()
        {
            //https://dev.twitter.com/web/sign-in/implementing
            //https://dev.twitter.com/oauth/overview/authorizing-requests

            //Step:1 Obtaining a request token
            HttpClient client = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage();
            request.RequestUri = new Uri("https://api.twitter.com/oauth/request_token");
            request.Method = HttpMethod.Post;

            OAuthBase _oAuthBase = new OAuthBase();

            string callback_url = "http://127.0.0.1:53284/sv/Products/Callback";

            string normalizedUri;
            string normalizedParameters;
            string authHeader;

            string signature = _oAuthBase.GenerateSignature(
                request.RequestUri,
                callback_url,
                oauthconsumerkey,
                oauthconsumersecret,
                null,
                null,
                request.Method.Method,
                _oAuthBase.GenerateTimeStamp(),
                _oAuthBase.GenerateNonce(),
                out normalizedUri,
                out normalizedParameters,
                out authHeader);

            request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", authHeader);

            var response = client.SendAsync(request).Result;
            if (!response.IsSuccessStatusCode)
                return null;

            string message = response.Content.ReadAsStringAsync().Result;
            NameValueCollection qs = HttpUtility.ParseQueryString(message);
            OAuth_Token oauth_token = new OAuth_Token();

            if (qs["oauth_token"] != null)
                     oauth_token.oauth_token = qs["oauth_token"];
            if (qs["oauth_token_secret"] != null)
                oauth_token.oauth_token_secret = qs["oauth_token_secret"];
            if (qs["oauth_callback_confirmed"] != null && qs["oauth_callback_confirmed"].Equals("true"))
                oauth_token.oauth_callback_confirmed = true;

            return oauth_token;
        }
Ejemplo n.º 6
0
        public OAuth_Token OAuth_Convert_Request_To_Access_Token(OAuth_Token token)
        {
            HttpClient client = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage();
            request.RequestUri = new Uri("https://api.twitter.com/oauth/access_token");
            request.Method = HttpMethod.Post;

            OAuthBase _oAuthBase = new OAuthBase();

            string normalizedUri;
            string normalizedParameters;
            string authHeader;

            string signature = _oAuthBase.GenerateSignature(
                request.RequestUri,
                null,
                oauthconsumerkey,
                oauthconsumersecret,
                token.oauth_token,
                null,
                request.Method.Method,
                _oAuthBase.GenerateTimeStamp(),
                _oAuthBase.GenerateNonce(),
                out normalizedUri,
                out normalizedParameters,
                out authHeader);

            request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", authHeader);
            request.Content = new StringContent("oauth_verifier="+token.oauth_token_secret, Encoding.UTF8);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

            var response = client.SendAsync(request).Result;
            if (!response.IsSuccessStatusCode)
                return null;

            string message = response.Content.ReadAsStringAsync().Result;
            NameValueCollection qs = HttpUtility.ParseQueryString(message);
            OAuth_Token oauth_token = new OAuth_Token();
            if (qs["oauth_token"] != null)
                oauth_token.oauth_token = qs["oauth_token"];
            if (qs["oauth_token_secret"] != null)
                oauth_token.oauth_token_secret = qs["oauth_token_secret"];

            return oauth_token;
        }
Ejemplo n.º 7
0
        public OAuth_Token OAuth_Request_Token()
        {
            //https://dev.twitter.com/web/sign-in/implementing
            //https://dev.twitter.com/oauth/overview/authorizing-requests

            //Step:1 Obtaining a request token
            HttpClient client = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage();

            request.RequestUri = new Uri("https://api.twitter.com/oauth/request_token");
            request.Method     = HttpMethod.Post;

            OAuthBase _oAuthBase = new OAuthBase();

            string callback_url = "http://127.0.0.1:53284/sv/Products/Callback";

            string normalizedUri;
            string normalizedParameters;
            string authHeader;

            string signature = _oAuthBase.GenerateSignature(
                request.RequestUri,
                callback_url,
                oauthconsumerkey,
                oauthconsumersecret,
                null,
                null,
                request.Method.Method,
                _oAuthBase.GenerateTimeStamp(),
                _oAuthBase.GenerateNonce(),
                out normalizedUri,
                out normalizedParameters,
                out authHeader);

            request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", authHeader);

            var response = client.SendAsync(request).Result;

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            string message                  = response.Content.ReadAsStringAsync().Result;
            NameValueCollection qs          = HttpUtility.ParseQueryString(message);
            OAuth_Token         oauth_token = new OAuth_Token();

            if (qs["oauth_token"] != null)
            {
                oauth_token.oauth_token = qs["oauth_token"];
            }
            if (qs["oauth_token_secret"] != null)
            {
                oauth_token.oauth_token_secret = qs["oauth_token_secret"];
            }
            if (qs["oauth_callback_confirmed"] != null && qs["oauth_callback_confirmed"].Equals("true"))
            {
                oauth_token.oauth_callback_confirmed = true;
            }

            return(oauth_token);
        }