Flickr API: How to perform Authentication using PHP

I presented this at Brighton BArCamp3 download power point

//1.Create a login link

$secret_key=’secret key’;

$params = array(
‘api_key’=> ‘api_key’,
‘perms’ => ‘write’);

$encoded_param = array();

//1.a Encode and organize authentication parameters

foreach($params as $key => $value)
{
$encoded_params[] = urlencode($key).’=’.urlencode($value);
}

//1b Create the api signature :an md5 of the parameters in alphabetic order


$apisig=md5($secret_key.’api_key’.$params[‘api_key’].’perms’.$params[‘perms’]);

//1.c append api signature to url

$url =”http://flickr.com/services/auth/?”.

implode(‘&’,$encoded_params).”&api_sig=”.$apisig;

header(‘Location:’.$url); –>This will then redirect back to your call back url and include a frob value

//2.Get and use frob to retrive authentication token

//2.a Get frob from url

if ($_REQUEST[‘frob’])
{
//using php_serial paramater serial to retrieve the format in serialized data structure
$frobparams = array(
‘api_key’=> ‘api_key’,
‘frob’ => ‘frob’ ,
‘method’ => ‘flickr.auth.getToken’,
‘format’ => ‘php_serial’);

$encoded_param = array();

foreach($frobparams as $key => $value)
{
$encoded_params[] = urlencode($key).’=’.urlencode($value);
}
$url = “http://api.flickr.com/services/rest/?”.implode(‘&’, $encoded_params);

//2.bCreate the api signature :an md5 of the parameters in alphabetic order

$apisigfrob=md5(

$secret_key.

‘api_key’. $frobparams [‘api_key’].

‘format’.$frobparams [‘format’].

‘frob’.$frobparam[‘frob’].

‘method’.$frobparams[‘method’]);

//2.c Construct url to auth.getToken

$url=$url.”&api_sig=”.$apisigfrob;

// 2.d Get url response/contents
$rsp = file_get_contents($url);

$rsp_obj = unserialize($rsp);

$details=$rsp_obj;

//2.e Glean Auth Token from $details

if($details[stat]==’ok’)
{
echo $details[‘auth’][‘token’][‘_content’];
}
else
{

//dispaly error message if there is a problem
echo $details [message] ;

}

}

Just getting started: The Authentication Token can then be ussed as part of the parameter for a method call

Leave a Reply

Your email address will not be published. Required fields are marked *