Flickr Authentication

Authentication

In order to communicate with the Flickr API, you need to create an app via Flickr's App Garden (or use one of your existing apps).

Once you have an app, it will have a key and a secret as shown below (the actual page will have real values):

image

If you just need to access public data via the Flickr API, the key and secret is sufficient (using the OAuth 1.0a terminology, these are called the consumer key and consumer secret). You can then start calling the API like:

// Initialize a new instance of "FlickrService"
FlickrService service = FlickrService.CreateFromConsumerKey(
    "The consumer key of your app",
    "The consumer secret of your app"
);
    
// Start making calls to the API

If you need to access the API on behalf of a specific user, you also need to obtain an access token and access token secret on behalf of that user. With the these obtained, you can start calling the API like:

// Initialize a new instance of "FlickrService"
FlickrService service = FlickrService.CreateFromAccessToken(
    "The consumer key of your app",
    "The consumer secret of your app",
    "The access token of the user",
    "The access token secret of the user"
);

// Start making calls to the API

Setting up an authentication

The below shows how you can set up an authentication page in a Razor view:

@using Skybrud.Social.Flickr.Exceptions
@using Skybrud.Social.Flickr.OAuth
@using Skybrud.Social.OAuth.Objects
@using Skybrud.Social.OAuth.Responses

@{

    // Initialize a new OAuth client with information about your app
    FlickrOAuthClient oauth = new FlickrOAuthClient {
        ConsumerKey = "The consumer key of your app",
        ConsumerSecret = "The consumer secret of your app",
        Callback = "http://social.abjerner/flickr/oauth/"
    };


    if (Request.QueryString["do"] == "login") {

        try {

            // Get a request token from the Flickr API
            SocialOAuthRequestTokenResponse response = oauth.GetRequestToken();

            // Get the token from the response
            SocialOAuthRequestToken token = response.Body;

            // Save the token information to the session so we can grab it later
            Session[token.Token] = token;

            // Redirect the user to the authentication page at Twitter.com
            Response.Redirect(token.AuthorizeUrl + "&perms=read");

        } catch (FlickrOAuthException ex) {

            <div class="alert alert-danger">
                <strong>Authentication failed</strong><br />
                @ex.Problem
                @*<pre>@ex.GetType().FullName: @(ex.Message + "\r\n\r\n" + ex.StackTrace)</pre>*@
            </div>

        } catch (Exception ex) {

            <div class="alert alert-danger">
                <strong>Authentication failed</strong><br />
                @ex.Message
                @*<pre>@ex.GetType().FullName: @(ex.Message + "\r\n\r\n" + ex.StackTrace)</pre>*@
            </div>

        }

    } else if (Request.QueryString["oauth_token"] != null) {

        // Get OAuth parameters from the query string
        string oAuthToken = Request.QueryString["oauth_token"];
        string oAuthVerifier = Request.QueryString["oauth_verifier"];

        // Grab the request token from the session
        SocialOAuthRequestToken token = Session[oAuthToken] as SocialOAuthRequestToken;

        if (token == null) {

            <p>An error occured. Timeout?</p>

        } else {

            // Some information for development purposes
            <p>Request Token: @token.Token</p>
            <p>Request Token Secret: @token.TokenSecret</p>

            // Update the OAuth client with information from the request token
            oauth.Token = token.Token;
            oauth.TokenSecret = token.TokenSecret;

            try {

                // Obtain an access token from the request token and OAuth verifier
                SocialOAuthAccessToken accessToken = oauth.GetAccessToken(oAuthVerifier).Body;

                // Update the OAuth client with the access token and access token secret
                oauth.Token = accessToken.Token;
                oauth.TokenSecret = accessToken.TokenSecret;

                // Some information for development purposes
                //Content.Text += "<b>Hi " + (user.Name ?? user.ScreenName) + "</b> (" + user.Id + ")<br />";
                <p>Access Token: @accessToken.Token</p>
                <p>Access Token Secret: @accessToken.TokenSecret</p>

            } catch (FlickrOAuthException ex) {

                <div class="alert alert-danger">
                    <strong>Authentication failed</strong><br />
                    @ex.Problem
                    @*<pre>@ex.GetType().FullName: @(ex.Message + "\r\n\r\n" + ex.StackTrace)</pre>*@
                </div>

            } catch (Exception ex) {

                <div class="alert alert-danger">
                    <strong>Authentication failed</strong><br />
                    @ex.Message
                    @*<pre>@ex.GetType().FullName: @(ex.Message + "\r\n\r\n" + ex.StackTrace)</pre>*@
                </div>

            }

        }

    } else if (Request.QueryString["denied"] != null) {

        // Get OAuth parameters from the query string
        string oAuthToken = Request.QueryString["denied"];

        // Remove the request token from the session
        Session.Remove(oAuthToken);

        // Write some output for the user
        <p>It seems that you cancelled the login!</p>
        <p>
            <a class="btn btn-primary" href="/flickr/oauth/?do=login">Try again?</a>
        </p>

    } else {

        <p>
            <a class="btn btn-primary" href="/flickr/oauth/?do=login">Login with Flickr</a>
        </p>

    }

}