AuthPack Provides .NET oAuth for Twitter, Facebook, LinkedIn, and Google

November 24, 201146 Comments

I’ve published a project on GitHub that includes examples for authenticating users with some of the major social sites. LinkedIn and Twitter have very similar oAuth implementations so I was able to leverage all of my earlier Twitter .NET oAuth work.

Code–> AuthPack on GitHub

Most of the code comes from the standard Web Forms project template. I wove in the authentication code which uses Forms Auth without an auth provider. You’ll need to add your own business logic and error handling, but it’s a good start.

I wish I had more time to create an elegant visual interface for the buttons, but maybe someone else will provide an update.

There is also a BSD-style license attached, which means you can do what you want with it, but please leave the copyright notice intact. That seemed to come up a few times with the old code where people weren’t sure if they could use it or not. A funny thing happened while I was researching code for LinkedIn. It seems that Fatih repackaged my Twitter work as his own work on LinkedIn. Dude, at least change the comments. :)

Share

46 Responses to “AuthPack Provides .NET oAuth for Twitter, Facebook, LinkedIn, and Google”

  1. adam says:

    I am using your excellent library but whenever I make an oauth call to an API method like http://api.twitter.com/1/users/lookup.xml?user_id=XXX,XXX,XXX it always applies the rate limit to the 150 requests/hour instead of 350/hr for an oauth request. After 150 requests then it dies until the rate limit allows more requests. Should your library using the oAuthWebRequest(…) method allow for an oauth request and get 350 requests/hr? If so, is there anything different that has to be done outside of setting the secrets and tokens?

  2. Shannon Whitley says:

    Hi Adam,

    Since the lookup method doesn’t require authentication, you may have a problem with your setup. Try using a method that requires authentication (http://api.twitter.com/1/statuses/retweeted_by_me.xml) and troubleshoot from there. Are you setting your user tokens before the call?

  3. How can I stream Twitter Feeds to my Facebook page in real-time, without any delay? I used to use “Smart Tweets For Pages” in Facebook, but now, I have to administer the Twitter pages that I want to feed to my Facebook page. RSS Graffiti is delayed a little bit. What application can I use that does this instantly?

  4. moe says:

    i get this error {“error”:{“message”:”An active access token must be used to query information about the current user.”,”type”:”OAuthException”}} facebook

  5. Paul says:

    Hi,
    VS.net 2010, framework 4.0

    Thanks for the great example! I added the keys to the wb.config and ran it. (almost too easy).

    A question I have is:
    When I run this app (twitter), I keep getting prompted by twitter to authorize the app. Clicking the “Authorize app” button successfully does so. Is there something, in addition to sending the consumerKey and consumerSecret, additional we need to do to get the app to successfully authorize without the need for a manual authorization?

    Thanks in advance,
    Paul

  6. Shannon Whitley says:

    @Paul — Once you store the user token and tokensecret, you can reuse those values to connect with Twitter without additional authorization. Just assign the token and tokensecret to the oAuth object. when you want to make another Twitter call.

  7. moe says:

    `hi,

    what is the code to get profile avatar for twitter and post to twitter ?

    thanks

  8. @moe – I updated the code to display avatars. The post to Twitter code was already there.

  9. moe says:

    thanks for avatar

    what is the complete code for post to twitter? i cant find it and get it to work
    //POST Test
    string url = “http://api.twitter.com/1/statuses/update.xml”;
    String xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.POST, url, “status=” + oAuth.UrlEncode(“Hello @swhitley – Testing the .NET oAuth API”));
    apiResponse.InnerHtml = Server.HtmlEncode(xml) ???

    • Shannon says:

      Hi moe, That is the complete code. If it’s not working for you, you’ll want to walkthrough the code in debug mode and check the return values and exceptions.

  10. moe says:

    can you post the complete code ? Shannon

  11. Shannon says:

    Hi @moe – When I said “this” code, I was referring to the code you posted. As long as you have updated the oAuth object with the user’s token and token secret, the code that you posted will post to Twitter.
    ======================
    AuthTwitter oAuth = new AuthTwitter(); //Assumes Consumer Key & Secret in web.config
    oAuth.token = {user’s oAuth token};
    oAuth.token_secret = {user’s oAuth secret};

    //POST Test
    string url = “http://api.twitter.com/1/statuses/update.xml”;
    String xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.POST, url, “status=” + oAuth.UrlEncode(“Hello @swhitley – Testing the .NET oAuth API”));
    =======================
    That’s it!

  12. moe says:

    oAuthTwitter oAuth = new oAuthTwitter(); //Assumes Consumer Key & Secret in

    oAuth. Token = ” “; / / i puted my tokens
    oAuth.TokenSecret = “”;
    string url = “http://api.twitter.com/1/statuses/update.xml”;
    String xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.POST, url, “status=” + oAuth.UrlEncode(txtdesp.Text));

    error message
    System.Exception: Could not authenticate you. /1/statuses/update.xml at AuthUtilities.WebResponseGet(HttpWebRequest webRequest) in

    please tell me why

  13. There are several things to look for:

    1) Double-check your Consumer Key and Consumer Secret in web.config against the setup on Twitter. Have you been able to GET data from Twitter (users/show)?
    2) “Update” requires that your Twitter application have read/write access. Is that configured on Twitter?
    3) Make sure your clock on your machine is accurate.
    4) Double-check oAuth.Token and oAuth.TokenSecret — Make sure you’re using the correct ~user~ tokens (not the application tokens).
    5) During your first test, use a simple, English word in your update text to eliminate the possibility that the text is causing trouble.
    6) Do not post duplicate text. Twitter rejects duplicates.

    Those are all of the things that can go wrong. The Exception seems to indicate that you aren’t using the correct tokens. I’d focus on that.

  14. moe says:

    the code works

    i used the access tokens keys instead of consumers keys

  15. artur says:

    Hello,

    Excelent work! Congrats!
    Where can I find the copyright notice I have to include on my site/code?

    Regards

  16. Shannon Whitley says:

    Thanks. There’s a copyright notice in Auth.aspx.cs. If you just use snippets of the code, then there’s no requirement to maintain the notice, although it’s always good form to include the names of authors from which you’ve borrowed.

  17. rohit says:

    i have error when using twitter login api

    Server Error in ‘/sample’ Application.

    Parser Error

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

    Parser Error Message: Could not load type ‘oAuthExample._Default’.

    Source Error:

    Line 1:
    Line 2:
    Line 3:

  18. shailesh says:

    Hello,

    I have downloaded AuthPack API and Integrated Into My Site.
    It is working properly but the response is redirecting to my root website instead of callback URL

    My Callback URL Is : http://www.pavansgroup.com/authpack/
    But it Redirects to http://www.pavansgroup.com

    Can you please help me to solve this problem.

    Thanks!

  19. Shannon says:

    @Rohit – Check the namespace for your default class. You have a mismatch in default.aspx.

    @shailesh –

    Sorry about that. I guess the javascript assumes the website is at the root. Please make the following change.

    Open /auth/auth.aspx.cs and look for the following code:


    OLD: Response.Write("<script type='text/javascript'>window.opener.location.href = '/';window.close();</script>");

    NEW: Response.Write("<script type='text/javascript'>window.opener.location.reload();window.close();</script>");

  20. milind says:

    hi…..

    my app is giving an error
    Server Error in ‘/sample’ Application.
    Parser Error Message: Could not load type ‘oAuthExample._Default’.
    actually error due to namespace how to deal with plz explain in detail its urgent for me..plz sir help me…..

  21. Sehab says:

    How can I get the Post ID once I’ve posted to Twitter.

  22. @Sehab – I believe the full post is returned to you with the id following your update.

  23. Sehab says:

    Thanks. Works great.

  24. ram says:

    hi…

    if I login with facebook then how to singout from this please help.

  25. @ram – Take a look at this link – http://stackoverflow.com/questions/2764436/facebook-oauth-logout

    On Contaxio, I don’t log my users out of Facebook. I provide them with a link where they can choose to logout. It’s basic javascript for the logout.

  26. ram says:

    thanks Shannon..it works…

    how can i give you credit on my website?

    thnks again

  27. @ram – I’m glad to hear it worked, that’s thanks enough.

  28. ram says:

    Hi…

    How can I post only once on user fb wall at login time?

  29. @ram – Sorry Ram, that’s outside the scope of this project.

  30. Shaukat says:

    facebook button is not displayed. i tried bypassing to facebook code in auth.aspx.cs and it gives me error on Request[fbsr_ ...]. Tried looking for where you have set the value for it but couldn’t find it. Any idea please ?

  31. @Shaukat – Make sure your Facebook application is setup properly on Facebook. You have to tell Facebook the url of the website where the code will display the button.

  32. Robert says:

    The below lines are now not working. I think it may be linked to Twitter going down yesterday. There is no data coming through.

    url = “https://api.twitter.com/1/account/verify_credentials.json”;
    TwitterUser user = Json.Deserialise(oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty));

    any ideas?

  33. @Robert – Check the line again. It looks like you removed the Json class from the Deserialize method:

    TwitterUser user = Json.Deserialise<TwitterUser>(oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty));

    Although after posting this, I realize it might just be WordPress removing the code from your comment.

  34. Robert says:

    @Shannon – WordPress removed the code from my comment.

    If your code is still working then I must have changed something by mistake to make my code not work.

    Any ideas on where a code change might effect the two lines in my previous post?

    Sorry for bothering you. Your help is much appreciated.

  35. Shannon says:

    @Robert – Please send me an email: swhitley at whitleymedia.com

    I’ll try to help you fix this.

  36. Robert says:

    I have emailed you. My email is: robertmadd@gmail.com

  37. Jank says:

    Hi do you know if this works with the new twitter API 1.1 https://dev.twitter.com/blog/current-status-api-v1.1

    I’ve heard that when you register a new application now it requires the use of OAuth 1.1.

    Thanks for this code!

  38. Shannon Whitley says:

    Hi Jank,

    That’s a good question. I haven’t tested the 1.1 API yet, so I’m not sure. Let me know what you find and I’ll do the same.

  39. Jank says:

    Yes, It works. I did change the twitter url in your code to use https as twitter recommends: https://api.twitter.com/1/ and it works fine as well.

    During the Twitter registration of my application I changed the access to read/write so I can tweet updates after authorized.

    Now I’m going to put some code together to send a tweet.

  40. @Jank

    Yes, I’m working through it now. For some of my other code (outside of this project), it looks like the biggest change is moving to json where xml is no longer an option.

  41. Venkaatesh says:

    hi , can u pls help me out in providing entire code for logging in to twitter from my own website which is on the verge of creation.

  42. Jayakumar says:

    hi
    Shannon Whitley when i login linkedin but when i login my iframe closed.

  43. Jayakumar says:

    Hi
    Shannon Whitley

    When i click linkedin button open new window i put my username and pwd
    then click allow access my window closed and nothing happen can u solve this

  44. I wanted to thank you for this great read. I definitely enjoyed every little bit of it. I have you bookmarked your site to check out the latest stuff you post.

  45. It isn’t that difficult to bring your site back up to the top. You can beat Panda, Penguin and Hummingbird algorithm changes. Win the Google Update War.

Leave a Reply

Twitter Tweet This