Twitter Authentication

Interestingly, someone knows the working Twitter sign-in model (OAuth) for .NET.

I am currently using this http://www.voiceoftech.com/swhitley/?p=681

but it only works if I set the callback url to “oob”, if I set the real return url, I get “401 unauthorized”.

Thanks!

+7
source share
5 answers

My problem has already been resolved with http://www.voiceoftech.com/swhitley/?p=681

I saved my application as a “browser”, but since I did not specify a callback URL, it was converted to a “client” application when saving.

+2
source

I wrote OAuth for this manager because the existing options were too complex.

OAuth with validation in .NET

The class focuses on OAuth and works specifically with Twitter. This is not a class that provides a ton of methods for the entire surface of the Twitter web API. This is just OAuth. If you want to update the status on Twitter, this class does not provide the "UpdateStatus" method. I decided that application developers just needed to build the HTTP message that they want to send. In other words, an HTTP message is an API. But OAuth stuff can get a little complicated, so it deserves an API, which is an OAuth class.

Here's an example code for requesting a “request token”:

var oauth = new OAuth.Manager(); oauth["consumer_key"] = MY_APP_SPECIFIC_CONSUMER_KEY; oauth["consumer_secret"] = MY_APP_SPECIFIC_CONSUMER_SECRET; oauth.AcquireRequestToken(SERVICE_SPECIFIC_REQUEST_TOKEN_URL, "POST"); 

THIS . On Twitter, the request url for tokens related to services is " https://api.twitter.com/oauth/request_token ".

Once you receive the request token, you will choose a web browser user interface in which the user will explicitly give you approval for your application to access Twitter. You need to do this once, the first time you launch the application. Do this in the built-in WebBrowser control with the following code:

 var url = SERVICE_SPECIFIC_AUTHORIZE_URL_STUB + oauth["token"]; webBrowser1.Url = new Uri(url); 

For Twitter, the URL for this is: https://api.twitter.com/oauth/authorize?oauth_token= "with the addition of oauth_token.

Take the pin from the web browser user interface using some screenshots of the HTML screen. Then request the "access token":

 oauth.AcquireAccessToken(URL_ACCESS_TOKEN, "POST", pin); 

For Twitter, this URL is " https://api.twitter.com/oauth/access_token ".

You do not need to explicitly handle the access token; the OAuthManager class keeps it in a state for you. But tokens and a secret are available in oauth["token"] and oauth["token_secret"] if you want to write them to persistent storage. To make requests with this access token, generate the authz header as follows:

 var authzHeader = oauth.GenerateAuthzHeader(url, "POST"); 

... where url is the endpoint of the resource. To update the status of a user on Twitter, it will be " http://api.twitter.com/1/statuses/update.xml?status=Hello ".

Then set the resulting string to the HTTP header named Authorization and send the HTTP request to the URL.

In subsequent runs, when you already have an access token and a secret, you can create an OAuth.Manager instance as follows:

 var oauth = new OAuth.Manager(); oauth["consumer_key"] = MY_APP_SPECIFIC_CONSUMER_KEY; oauth["consumer_secret"] = MY_APP_SPECIFIC_CONSUMER_SECRET; oauth["token"] = your_stored_access_token; oauth["token_secret"] = your_stored_access_secret; 

Then just generate the authz header and execute your queries as described above.

Download DLL
Browse Documentation

+5
source

I'm late for the conversation, but I created a video tutorial for everyone who has the same task. Like you, I had fun finding out error 401.

Video: http://www.youtube.com/watch?v=TGEA1sgMMqU

Tutorial: http://www.markhagan.me/Samples/Grant-Access-And-Tweet-As-Twitter-User-ASPNet

Code (if you do not want to leave this page):

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Twitterizer; namespace PostFansTwitter { public partial class twconnect : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var oauth_consumer_key = "gjxG99ZA5jmJoB3FeXWJZA"; var oauth_consumer_secret = "rsAAtEhVRrXUTNcwEecXqPyDHaOR4KjOuMkpb8g"; if (Request["oauth_token"] == null) { OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken( oauth_consumer_key, oauth_consumer_secret, Request.Url.AbsoluteUri); Response.Redirect(string.Format("http://twitter.com/oauth/authorize?oauth_token={0}", reqToken.Token)); } else { string requestToken = Request["oauth_token"].ToString(); string pin = Request["oauth_verifier"].ToString(); var tokens = OAuthUtility.GetAccessToken( oauth_consumer_key, oauth_consumer_secret, requestToken, pin); OAuthTokens accesstoken = new OAuthTokens() { AccessToken = tokens.Token, AccessTokenSecret = tokens.TokenSecret, ConsumerKey = oauth_consumer_key, ConsumerSecret = oauth_consumer_secret }; TwitterResponse<TwitterStatus> response = TwitterStatus.Update( accesstoken, "Testing!! It works (hopefully)."); if (response.Result == RequestResult.Success) { Response.Write("we did it!"); } else { Response.Write("it all bad."); } } } } } 
+2
source

"DotNetOpenAuth" will be a great helper for u. http://www.dotnetopenauth.net/

+1
source

I developed a C # library for OAuth that is really easy to use and gets up and running. The project is an open source project and I have included a demo application that works against 1. Google 2. Twitter 3. Yahoo 4. Vimeo

Of course, any other OAuth provider will also do this. Here you can find an article and a library.

OAuth C # Library

+1
source

All Articles