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):
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:
FlickrService service = FlickrService.CreateFromConsumerKey(
"The consumer key of your app",
"The consumer secret of your app"
);
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:
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"
);
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
@{
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 {
SocialOAuthRequestTokenResponse response = oauth.GetRequestToken();
SocialOAuthRequestToken token = response.Body;
Session[token.Token] = token;
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) {
string oAuthToken = Request.QueryString["oauth_token"];
string oAuthVerifier = Request.QueryString["oauth_verifier"];
SocialOAuthRequestToken token = Session[oAuthToken] as SocialOAuthRequestToken;
if (token == null) {
<p>An error occured. Timeout?</p>
} else {
<p>Request Token: @token.Token</p>
<p>Request Token Secret: @token.TokenSecret</p>
oauth.Token = token.Token;
oauth.TokenSecret = token.TokenSecret;
try {
SocialOAuthAccessToken accessToken = oauth.GetAccessToken(oAuthVerifier).Body;
oauth.Token = accessToken.Token;
oauth.TokenSecret = accessToken.TokenSecret;
<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) {
string oAuthToken = Request.QueryString["denied"];
Session.Remove(oAuthToken);
<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>
}
}