×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Using Flickr API in FlashLite

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Bot fixing redirect link)
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
(One intermediate revision by one user not shown)
Line 1: Line 1:
{{FeaturedArticle}}
+
[[Category:Flash Lite]][[Category:Web Services]][[Category:Code Examples]]
[[Category:Flash_Lite]]
+
{{FeaturedArticle|timestamp=20100606}}
[[Category:Code Examples]]
+
 
=Introduction=
+
==Introduction==
 
Flickr has an open Application Programming Interface (API). This means that anyone can write their own program to present public Flickr data (like photos, video, tags, profiles or groups) in new and different ways. This article describes how to use the Flickr’s API and create an application from Flash Lite. You can get the details about the API and documentation at [http://www.flickr.com/services/api/ FlickrAPI].
 
Flickr has an open Application Programming Interface (API). This means that anyone can write their own program to present public Flickr data (like photos, video, tags, profiles or groups) in new and different ways. This article describes how to use the Flickr’s API and create an application from Flash Lite. You can get the details about the API and documentation at [http://www.flickr.com/services/api/ FlickrAPI].
  
Line 8: Line 8:
 
First of all, to develop the Flickr applications, you must have a '''Flickr key''', which is associated with the application you are developing. Once you have logged on the Flickr website, you can apply for the API key at [https://login.yahoo.com/config/login?.src=flickrsignin&.pc=8190&.scrumb=0&.pd=c%3DJvVF95K62e6PzdPu7MBv2V8-&.intl=us&.done=https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickrsignin%26.pc%3D8190%26.scrumb%3D0%26.pd%3Dc%253DJvVF95K62e6PzdPu7MBv2V8-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F%253Fredir%253D%25252Fservices%25252Fapps%25252Fcreate%25252Fapply%25252F FlickrKey].
 
First of all, to develop the Flickr applications, you must have a '''Flickr key''', which is associated with the application you are developing. Once you have logged on the Flickr website, you can apply for the API key at [https://login.yahoo.com/config/login?.src=flickrsignin&.pc=8190&.scrumb=0&.pd=c%3DJvVF95K62e6PzdPu7MBv2V8-&.intl=us&.done=https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickrsignin%26.pc%3D8190%26.scrumb%3D0%26.pd%3Dc%253DJvVF95K62e6PzdPu7MBv2V8-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F%253Fredir%253D%25252Fservices%25252Fapps%25252Fcreate%25252Fapply%25252F FlickrKey].
  
{{Note| Please read the terms and conditions of using the Flickr API before using them. https://login.yahoo.com/config/login?.src=flickrsignin&.pc=8190&.scrumb=0&.pd=c%3DJvVF95K62e6PzdPu7MBv2V8-&.intl=us&.done=https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickrsignin%26.pc%3D8190%26.scrumb%3D0%26.pd%3Dc%253DJvVF95K62e6PzdPu7MBv2V8-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F%253Fredir%253D%25252Fservices%25252Fapi%25252Ftos%25252F}}
+
{{Note|Please read the Flickr API [https://login.yahoo.com/config/login?.src{{eq}}flickrsignin&.pc{{eq}}8190&.scrumb{{eq}}0&.pd{{eq}}c%3DJvVF95K62e6PzdPu7MBv2V8-&.intl{{eq}}us&.done{{eq}}https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickrsignin%26.pc%3D8190%26.scrumb%3D0%26.pd%3Dc%253DJvVF95K62e6PzdPu7MBv2V8-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F%253Fredir%253D%25252Fservices%25252Fapi%25252Ftos%25252F Terms of use] before using them. }}
  
 
=Using Flickr Mobile Authentication API=
 
=Using Flickr Mobile Authentication API=
Line 192: Line 192:
 
When you click on the button, the application will show you the title of all the photosets for this user.
 
When you click on the button, the application will show you the title of all the photosets for this user.
 
=Download=
 
=Download=
You can download the source code at [http://www.forum.nokia.com/piazza/wiki/images/8/8e/FlickrDemo.zip FlickrAPI]. This code is tested on Nokia 5800 XpressMusic.
+
You can download the source code at [[Media:FlickrDemo.zip]]. This code is tested on Nokia 5800 XpressMusic.
 +
 
 +
 
 +
{{ArticleMetaData
 +
|sourcecode=[[Media:FlickrDemo.zip]]
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= Nokia 5800 XpressMusic <!-- Devices tested against-->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|creationdate=24 May 2010
 +
|author=[[User:sangramp@cybage.com]]
 +
}}

Revision as of 08:42, 3 October 2012

Featured Article
06 Jun
2010

Contents

Introduction

Flickr has an open Application Programming Interface (API). This means that anyone can write their own program to present public Flickr data (like photos, video, tags, profiles or groups) in new and different ways. This article describes how to use the Flickr’s API and create an application from Flash Lite. You can get the details about the API and documentation at FlickrAPI.

Prerequisites

First of all, to develop the Flickr applications, you must have a Flickr key, which is associated with the application you are developing. Once you have logged on the Flickr website, you can apply for the API key at FlickrKey.

Note.pngNote: Please read the Flickr API Terms of use before using them.

Using Flickr Mobile Authentication API

All of the Flickr API requires the user authentication before the result is returned. As we are targeting the application for mobile, we will show using the Mobile Application Authentication.

Obtain an API Key

As mentioned before, you should get an API key for your application.

Configuring the API Key

Once you get the API key, it will appear in your 'accounts keys section' on Flickr. You need to configure that key and must set Mobile Application for your Authentication Type. Once you configure the key, you get an authentication URL. This is the same URL, you prompt the user to enter the credentials for authentication.

Obtain a mini-token

Before your application start making the API requests, it must be authenticated using the above authentication URL. The application should open this URL in a new page and the user should authenticate with his credentials. Once the user authenticates, it gives you a 9 digit code, termed as mini-token. The FlashLite application must provide a mechanism so that user can enter/input this mini-token to the application.

Get the FullToken

The above mini-token is used as input to get the fulltoken. For getting the fulltoken ‘flickr.auth.getFullToken’ method is used. In addition to the api_key and mini_token, this API call requires a signature. To generate the signature, we take the shared secret and then append the alphabetically sorted list of arguments. The resulting string is then processed to get the MD 5 sum. This is used as the api_sig.

Demonstration with Example Code

The following code will detail the process of authenticating and signing API call request.

Initialize

Add the following code to force the FlashLite application to run in fullScreen mode and with high quality.

fscommand2 ("DisableKeypadCompatibilityMode");
status = fscommand2 ("FullScreen", true);
_focusrect = false;
_root._quality = "HIGH";

Also include the ‘md5.as’ file to get the MD 5 sum of the parameters. This is necessary to sign the request.

#include "md5.as"

Declare the API key and the secret code

I have created a sample API key for this Demo purpose and assigned the associated API key details in the following variables.

var AuthURL:String = "http://www.flickr.com/auth-*****************";
var sAPIKey:String = "********************************";
var sSecretKey:String = "*****************";
var sMiniToken:String = "";
var sSignatureString:String = "";
var sMethodToCall:String;
var sMD5Signature;
var sFullToken:String;

Prompt the user for the Authentication

Once you are to start the authentication, you should open the authentication URL in a new window. Add an Authentication button (Get Mini Token) to the FlashLite application and add the following code for its click handler.

on (release) {
getURL (AuthURL, "_blank");
}

Also add one input text-field to the application where user can input the mini-token generated. Once you click on the ‘Get Mini Token’ button, it will open a URL in new window. This will ask for the Flickr credentials, after entering the details, it will give you a 9 digit mini token.

Getting the Full Token

The mini token should be exchanged to get the final and full token for all further API references. Add button on the stage with the caption 'getFullToken' and add the following code to the click handler.

on (release) {
fnGetFullTokenForAuthentication ();
}

Add one more button on the stage with the caption ‘flickr.photosets.getList’ and the following code to the click handler.

on (release) {
fnPhotosetsGetList();
}

Add the following functions to handle the authentication process on the main timeline.

function fnGetFullTokenForAuthentication ()
{
sMiniToken = txtInput.text;
sMethodToCall = "flickr.auth.getFullToken";
sSignatureString = sSecretKey + "api_key" + sAPIKey + "method" + sMethodToCall + "mini_token" + sMiniToken;
sMD5Signature = calcMD5 (sSignatureString);
var xXMLObject:XML = new XML ();
xXMLObject.ignoreWhite = true;
xXMLObject.onLoad = function (success)
{
if (success)
{
fnParseGetFullToken (xXMLObject);
}
else
{
fnXMLLoadingError ();
}
};
xXMLObject.load ("http://api.flickr.com/services/rest/?method=flickr.auth.getFullToken&api_key=" + sAPIKey + "&mini_token=" + sMiniToken + "&api_sig=" + sMD5Signature);
}
function fnXMLLoadingError ()
{
txtOutput.text = "Error in network connection.Please try after some time.";
}
 
function fnParseGetFullToken (xResponceData:XML)
{
var sResponceStatus:String = String (xResponceData.firstChild.attributes.stat);
if (sResponceStatus == "ok")
{
var nTotalNodes:Number = xResponceData.firstChild.childNodes[0].childNodes.length;
for (i = 0; i < nTotalNodes; i++)
{
var sNodeName:String = String (xResponceData.firstChild.childNodes[0].childNodes[i].nodeName);
if (sNodeName == "token")
{
sFullToken = String (xResponceData.firstChild.childNodes[0].childNodes[i].firstChild.nodeValue);
}
}
txtFullToken.text = sFullToken;
}
else
{
fnXMLLoadingError ();
}
}

After the user enters the exact mini token in the input text field, call the ‘fnGetFullTokenForAuthentication’ function. This will call the ‘flickr.auth.getFullToken’ API to get the final full token. This API requires the following parameters:

  • api_key
  • method
  • mini_token
  • api_sig


To get the api_sig, you need to arrange all the parameters in alphabetical sequence and get MD-5 sum of the resulting string. The md5.as file included contains the algorithm for getting the MD 5 sum of the string. The function named 'calcMD5' takes string as parameter and returns the MD 5 sum of it.

sSignatureString = sSecretKey + "api_key" + sAPIKey + "method" + sMethodToCall + "mini_token" + sMiniToken;
sMD5Signature = calcMD5 (sSignatureString);

Using XML, we will load the result and see if the API call was successful. If the mini-token is correct, the API returns the full-token. This full token is used in all further API calls. The full token returned is displayed in the text field.

flickr.photosets.getList

UsingFlickrAPI.JPG

This API returns the photosets belonging to the specified user. We will call this API to get the details of photosets of the user and show the reult in the text field. Add the following functions to handle this API call request.

function fnPhotosetsGetList ()
{
var xXMLObject:XML = new XML ();
xXMLObject.ignoreWhite = true;
xXMLObject.onLoad = function (success)
{
if (success)
{
fnParseGetListResponce (xXMLObject);
}
else
{
fnXMLLoadingError ();
}
};
sMethodToCall = "flickr.photosets.getList";
sSignatureString = sSecretKey + "api_key" + sAPIKey + "auth_token" + sFullToken + "method" + sMethodToCall;
sMD5Signature = calcMD5 (sSignatureString);
xXMLObject.load ("http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=" + sAPIKey + "&auth_token=" + sFullToken + "&api_sig=" + sMD5Signature);
}
function fnParseGetListResponce (xResponceData:XML)
{
var sResponceStatus:String = String (xResponceData.firstChild.attributes.stat);
if (sResponceStatus == "ok")
{
var sOutPut:String = "You Have Following Albums:: \n";
var nTotalNodes:Number = xResponceData.firstChild.childNodes[0].childNodes.length;
for (i = 0; i < nTotalNodes; i++)
{
var nInnerNodes:Number = xResponceData.firstChild.childNodes[0].childNodes[i].childNodes.length;
for (j = 0; j < nInnerNodes; j++)
{
if (xResponceData.firstChild.childNodes[0].childNodes[i].childNodes[j].nodeName == "title")
{
sOutPut = sOutPut + xResponceData.firstChild.childNodes[0].childNodes[i].childNodes[j].firstChild.nodeValue + "\n";
}
}
}
txtOutput.text = sOutPut;
}
else
{
fnXMLLoadingError ();
}
}

When you click on the button, the application will show you the title of all the photosets for this user.

Download

You can download the source code at Media:FlickrDemo.zip. This code is tested on Nokia 5800 XpressMusic.


Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 XpressMusic
Article
Created: sangramp@cybage.com (24 May 2010)
Last edited: hamishwillee (03 Oct 2012)
64 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×