📜 ⬆️ ⬇️

Twitter with your own hands on C #. Part 1: OAuth authorization

At one point, there was a desire to write a client program for Twitter microblogging networks, C # was chosen as the programming language.
Let me remind you that OAuth is an open authorization protocol that allows a third party to provide access to protected user resources, without the need to transfer her (third party) login and password. The third person in this case, of course, we will be with our application.
In principle, this guide is suitable for OAuth authorization, not only on Twitter, but also on any other portal that uses OAuth, for example, the well-known FriendFeed.

The OAuth authorization process itself is described in some detail at Habré.
In order to log in to the twitter account, we need to register our application in Twitter Developers .
We fill in the form and press the button Register application, then you will be transferred to the application page. There we are interested in two lines, namely the Consumer key and the Consumer secret - we need to save them in some way because we will need them soon.
And now a few theories. In fact, the authorization process can be divided into three stages:
1. Make a request to api.twitter.com/oauth/request_token in order to get the initial values ​​of oauth_token and oauth_secret
2. Having received these values, we make another request in the browser window, but already at api.twitter.com/oauth/authorize and the user will be prompted to enter their login / password from the twitter account, and then another form informing us that Wonderful application wants to use user account data and of course two buttons: for refusal and confirmation. When you click on the confirmation button, a page will open on which the PIN code will be written. The user must transfer the PIN code to our application.
3. And after the user enters the PIN in our application, we will need to make a third request to api.twitter.com/oauth/access_token to get the oauth_token and oauth_secret to be used to identify the user in the system.
We will do everything consistently and by the end of the article we should have an application that can pass OAuth authentication.
Now go to the process of writing the program.
Create a new console application.
So, first we need to make some kind of request about this type api.twitter.com/oauth/request_token?oauth_consumer_key= <?> & Oauth_signature_method = <?> & Oauth_signature = <?> & Oauth_timestamp = <?> & Oauth_timestamp = <?> & Oauth_timestamp = <?> & Oauth_timestamp = <?> & Oauth_timestamp = <? >
Where <?> Must be your values. For some values, I used the small but very successful OAuthBase.cs class .
Download it and put it in the folder with the application.

Since we will make requests and work with the class we just downloaded, we should have the following lines in the program header:
using System;<br> using OAuth;<br> using System.Net;<br> using System.IO;<br> using System.Text; <br><br> * This source code was highlighted with Source Code Highlighter .

Now we need to form the query string, assign its value to the request_url variable of the type and execute the first query.
// : <br> Uri uri = new Uri ( "http://api.twitter.com/oauth/request_token" );<br> string consumerKey = " consumerKey" ;<br> string consumerSecret = " consumerSecret" ;<br> // OAuthBase <br>OAuthBase oAuth = new OAuthBase();<br> // <br> string timeStamp = oAuth.GenerateTimeStamp();<br> string nonce = oAuth.GenerateNonce();<br> string normUri;<br> string normParams;<br> string sig = oAuth.GenerateSignature(uri, consumerKey, consumerSecret, string .Empty, string .Empty, "GET" , timeStamp, nonce, OAuth.OAuthBase.SignatureTypes.HMACSHA1, out normUri, out normParams);<br> // <br> string request_url = <br> "http://api.twitter.com/oauth/request_token" + "?" +<br> "oauth_consumer_key=" + consumerKey + "&" +<br> "oauth_signature_method=" + "HMAC-SHA1" + "&" +<br> "oauth_signature=" + sig + "&" +<br> "oauth_timestamp=" + timeStamp + "&" +<br> "oauth_nonce=" + nonce + "&" +<br> "oauth_version=" + "1.0" ;<br> Console .WriteLine( "Req: " + request_url); // <br> Console .WriteLine( "--------------------------------------------------------" );<br> // <br>HttpWebRequest Request = (HttpWebRequest) HttpWebRequest.Create(request_url);<br>HttpWebResponse Response = (HttpWebResponse)Request.GetResponse(); <br>StreamReader Reader = new StreamReader(Response.GetResponseStream(), Encoding .GetEncoding(1251));<br> string outline = Reader.ReadToEnd(); <br> Console .WriteLine( "Out: " + outline);<br> Console .WriteLine( "--------------------------------------------------------" );<br> // <br> char [] delimiterChars = { '&' , '=' };<br> string [] words = outline.Split(delimiterChars);<br> string oauth_token = words[1]; <br> string oauth_token_secret = words[3];<br> string oauth_callback_confirmed = words[5];<br> // <br> Console .WriteLine( "oauth_token = " + oauth_token); <br> Console .WriteLine( "oauth_token_secret = " + oauth_token_secret); <br> Console .WriteLine( "oauth_callback_confirmed = " + oauth_callback_confirmed);<br> Console .WriteLine( "--------------------------------------------------------" ); <br><br> * This source code was highlighted with Source Code Highlighter .

Now you can go to the second step, where the user will be prompted to enter a PIN code

// <br> // PIN <br>request_url = "http://api.twitter.com/oauth/authorize?oauth_token=" + oauth_token;<br> Console .WriteLine( "Req: " + request_url);<br> Console .WriteLine( "--------------------------------------------------------" );<br>System.Diagnostics.Process.Start(request_url); // PIN- <br> Console .Write( "Enter PIN: " );<br> string oauth_verifier = Console .ReadLine(); // oauth_verifier — PIN-. <br> Console .WriteLine( "--------------------------------------------------------" ); <br><br> * This source code was highlighted with Source Code Highlighter .

And the final request, the answer to which we will have to save and access the account data

// <br>request_url = <br> "http://api.twitter.com/oauth/access_token" + "?" +<br> "oauth_consumer_key=" + consumerKey + "&" +<br> "oauth_token=" + oauth_token + "&" +<br> "oauth_signature_method=" + "HMAC-SHA1" + "&" +<br> "oauth_signature=" + sig + "&" +<br> "oauth_timestamp=" + timeStamp + "&" +<br> "oauth_nonce=" + nonce + "&" +<br> "oauth_version=" + "1.0" + "&" +<br> "oauth_verifier=" + oauth_verifier;<br> Console .WriteLine( "Req: " + request_url);<br> Console .WriteLine( "--------------------------------------------------------" ); <br> // <br>Request = (HttpWebRequest) HttpWebRequest.Create(request_url);<br>Response = (HttpWebResponse)Request.GetResponse(); <br>Reader = new StreamReader(Response.GetResponseStream(), Encoding .GetEncoding(1251));<br>outline = Reader.ReadToEnd(); <br> Console .WriteLine( "Out: " + outline);<br> Console .WriteLine( "--------------------------------------------------------" );<br> // <br>words = outline.Split(delimiterChars);<br>oauth_token = words[1]; <br>oauth_token_secret = words[3];<br> string user_id = words[5];<br> string screen_name = words[7];<br> // <br> Console .WriteLine( "oauth_token = " + oauth_token); <br> Console .WriteLine( "oauth_token_secret = " + oauth_token_secret); <br> Console .WriteLine( "user_id = " + user_id);<br> Console .WriteLine( "screen_name = " + screen_name); <br><br> * This source code was highlighted with Source Code Highlighter .

That's all all the necessary data obtained. Communication with the Twitter API will be reviewed in the next article. The output of lines with values ​​are made to improve the visibility of the authorization process and its understanding. Lines with hyphens are inserted so that the displayed text does not merge into one continuous line in the eyes.

UPD: the author did get an invite for a habr on his own. Welcome shpaker !

Source: https://habr.com/ru/post/103365/

All Articles