Skip to content

Authenticate to smugmug with oAuth and c#

Some weeks ago, i tried to create a windows phone 7 smugmug app(photo browser). I had some problems with oAuth at the beginning, First, I tried the google oAuth implementation in c# with the result that the first request(AcquireRequestToken) worked, but the second request(AcquireAccessToken) failed with an invalid signature. Then i tried to implement it myself with the same result.

Even with the smugmug oAuth documentation, i found it quite hard to get it done. Some parts like the authorize parameters(public/full/read/modify) i had to figure out myself.

After some time, i found an c# oAuth class written for twitter access for cropperplugins.codeplex. With this code, i was at least able to get a proper AccessToken for smugmug. I've written a little demo application if someone else has the same problems.

To get this sample working, you only have to set you apiKey and secret. This sample will do: 1. Get a request token and a request token secret 2. Authorize the request token 3. With the request token secret, acquire a access token 4. This access token and access token secret are used for all further authentication, and should not expire until the user revokes them.

        private void AuthenticateSmugmug()
        {
            const string apiKey = "YOURAPIKEY";//apikey from your smugmug account
            const string secret = "YOUR_SECRET";//secret from your smugmug account

        Console.WriteLine("Create new instance of OAuth Manager");
        Manager manager = new Manager(apiKey, secret);

        Console.WriteLine("Aquire a request token from smugmug");
        OAuthResponse requestToken = manager.AcquireRequestToken("http://api.smugmug.com/services/oauth/getRequestToken.mg", "GET");

        string oauthToken = requestToken["oauth_token"];
        Console.WriteLine(string.Format("oauthToken: {0}", oauthToken));
        string oauthTokenSecret = requestToken["oauth_token_secret"];
        Console.WriteLine(string.Format("oauthTokenSecret: {0}", oauthTokenSecret));

        //Opens your default browser and authorize your request token
        //Parameters: Access=[Public|Full], Permissions=[Read|Add|Modify]
        System.Diagnostics.Process.Start(string.Format("http://api.smugmug.com/services/oauth/authorize.mg?oauth_token={0}&Access=Full&Permissions=Modify", oauthToken));

        Console.WriteLine("Please enter you credentials into the browser before continuing");
        Console.WriteLine("Press any key to continue...");
        Console.ReadLine();

        OAuthResponse accessToken = manager.AcquireAccessToken("http://api.smugmug.com/services/oauth/getAccessToken.mg", "GET", oauthTokenSecret);

        Console.WriteLine(string.Format("accessToken: {0}", accessToken["oauth_token"]));
        Console.WriteLine(string.Format("accessTokenSecret: {0}", accessToken["oauth_token_secret"]));
        Console.WriteLine("Press any key to quit the application");
        Console.ReadLine();
    }

This is the complete source code for the oAuth sample. It's c# written in Visual Studio 10: To get this sample working, just enter your Api Key and Secret. SmugmugOAuthSample.zip

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

wuttrain on :

Hi,

Thank you for the sample code. This is very helpful. I have a question. Do you have a sample code that uploads the image to smugmug after you get the tokens?

Thanks!

SecretsofPakistan on :

By joining our communities, advertising your business publishing an ad or simply by sharing this directory with your family and friends, you will be part of the nation who took the first step to digitalize, connect and enhance Pakistan. Additionally, this Directory is the best source for foreigners who would love to get to know more about Pakistan and its amazing culture. Pakistan Zindabad!

Best online shopping Store in Pakistan on :

I was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read. Leftover.pk is one of the Best Online Shopping Store in Pakistan where you can get 100% Original Branded & Non-Branded Women, Men, and Kids clothing with Up to 50% off.

Best online shopping Store in Pakistan on :

I was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read. Leftover.pk is one of the Best Online Shopping Store in Pakistan where you can get 100% Original Branded & Non-Branded Women, Men, and Kids clothing with Up to 50% off.

pi on :

I got lost good thing I found this site!

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Markdown format allowed
Form options