Esempio n. 1
0
        public void OAuthCheck()
        {
            OAuthTwitterResponseBuilder twitterAuth = new OAuthTwitterResponseBuilder();

            string url = "http://twitter.com/account/verify_credentials.xml";
            string xml = twitterAuth.GetResponse(WebMethod.GET, url, String.Empty);
            Console.Out.WriteLine(xml);
        }
Esempio n. 2
0
        public void OAuthAuthorizeSequence()
        {
            string REQUEST_TOKEN = "http://api.twitter.com/oauth/request_token";
            string AUTHORIZE = "http://api.twitter.com/oauth/authorize";
            string ACCESS_TOKEN = "http://api.twitter.com/oauth/access_token";

            //1. The application uses oauth/request_token to obtain a request token from twitter.com.

            OAuthTwitterResponseBuilder twitterAuth = new OAuthTwitterResponseBuilder();
            //Redirect the user to Twitter for authorization.
            //Using oauth_callback for local testing.
            string oAuthToken;
            string response = twitterAuth.GetResponse(WebMethod.GET, REQUEST_TOKEN, String.Empty);
            oAuthToken = "ERROR OCCURED NO oAuthToken in response";
            if (response.Length > 0)
            {
                //response contains token and token secret.  We only need the token.
                NameValueCollection qs = HttpUtility.ParseQueryString(response);
                if (qs["oauth_token"] != null)
                {
                    oAuthToken = qs["oauth_token"];
                }
            }

            //2. The application directs the user to oauth/authorize on twitter.com.
            string authURL= AUTHORIZE + "?oauth_token=" + oAuthToken;
            Console.Out.WriteLine("Please visit following url to authorize:");
            Console.Out.WriteLine(authURL);

            Console.ReadKey();

            //3. After obtaining approval from the user, a prompt on twitter.com will display a 7 digit PIN.
            //4. The user is instructed to copy this PIN and return to the appliction.
            //5. The application will prompt the user to enter the PIN from step 4.
            string PIN = Console.ReadLine();

            //6. The application uses the PIN as the value for the oauth_verifier parameter in a call to oauth/access_token which will verify the PIN and exchange a request_token for an access_token.
            twitterAuth.Token = oAuthToken;
            twitterAuth.OAuthVerifier = PIN;
            response = twitterAuth.GetResponse(WebMethod.GET, ACCESS_TOKEN, String.Empty);

            //7. Twitter will return an access_token for the application to generate subsequent OAuth signatures.
            if (response.Length > 0)
            {
                //Store the Token and Token Secret
                NameValueCollection qs = HttpUtility.ParseQueryString(response);
                if (qs["oauth_token"] != null)
                {
                    twitterAuth.Token = qs["oauth_token"];
                }
                if (qs["oauth_token_secret"] != null)
                {
                    twitterAuth.TokenSecret = qs["oauth_token_secret"];
                }
            }

            Console.Out.WriteLine("Old key :'" + oAuthToken + "'");
            Console.Out.WriteLine("New? key :'" + twitterAuth.Token + "'");
            Console.Out.WriteLine("TokenSecret  :'" + twitterAuth.TokenSecret + "'");
            Console.Out.WriteLine("Checking it worked");

            string url = "http://twitter.com/account/verify_credentials.xml";
            string xml = twitterAuth.GetResponse(WebMethod.GET, url, String.Empty);

            Console.Out.WriteLine(xml);

            Console.Out.WriteLine("Checking it worked again");
            xml = twitterAuth.GetResponse(WebMethod.GET, url, String.Empty);

            Console.Out.WriteLine(xml);
            Console.ReadKey();
        }