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

November 24, 201115 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

15 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

Leave a Reply

Twitter Tweet This