Flickr login

This tutorial will guild you how to flickr login, at first, you must have flickr account, create a application to receive client id and secret, for more details please see this tutorial. create flickr app.

After that, write down app_id and app_secret to get start with flickr login.

System requirements

  • PHP 5.4 or higher
  • APACHE 2.4.9 or higher
  • Netbean IDE

Project Structure

 flickr structure

Implement code

Create projecthelper.php

We wrote one tutorial about projecthelper.php, view more at here, if you were already read it, please add this function in file.

  • public static function get_flickr_config() {
    return $config = array(
    'client_id' => 'your-client-id',
    'client_secret' => 'client-secret',
    'redirect_uri' => self::get_base_url('flickr-callback.php'),
    'login' => self::get_base_url('flickr-login.php'),
    'session' => 'flickr_access_token'
    );
    }

Create flickr-login.php

This file will undertake two main tasks, check access_token in $_SESSION, if existed, send request to get user's profile, the opposite will create $authen_url for the user grant access your applications to connect to their accounts.

- Create $authen_url link

  • // define permission
    $perm = 'write';
    // text sig
    $text = $config['client_secret'] . 'api_key' . $config['client_id'] . 'perms' . $perm;
    // generate app sig
    $app_sig = md5($text);
    // authen dialog url
    $auth_url = 'http://flickr.com/services/auth/?' .
    'api_key=' . $config['client_id'] .
    '&perms=' . $perm .
    '&api_sig=' . $app_sig;
    echo '<a href="' . $auth_url . '" >Login with flickr</a>';

Use's profile is contained in access_token, just print it on the screen

  •  $access_token = $_SESSION[$config['session']];
    var_dump($access_token);

<?php

if (!session_id()) {
session_start();
}
include 'projecthelper.php';

$config = projecthelper::get_flickr_config();
if (isset($_SESSION[$config['session']])) {
$access_token = $_SESSION[$config['session']];
var_dump($access_token);
} else {
// define permission
$perm = 'write';
// text sig
$text = $config['client_secret'] . 'api_key' . $config['client_id'] . 'perms' . $perm;
// generate app sig
$app_sig = md5($text);
// authen dialog url
$auth_url = 'http://flickr.com/services/auth/?' .
'api_key=' . $config['client_id'] .
'&perms=' . $perm .
'&api_sig=' . $app_sig;
echo '<a href="' . $auth_url . '" >Login with flickr</a>';
}
?>

Create flickr-callback.php

After granted access to your application, user will be redirected to your redirect_uri with authentication code. Then you will make request to get access_token.
(In some cases, your redirect_uri that you provide must match with the one you've described in app's settings, or redirect_uri must begin with https).

+ Check and extract authentication code

  • Send request to get access_token
  •  $frob = $_GET['frob'];
    $method = 'flickr.auth.getToken';
    $format = 'json';
    $nojsoncallback = 1;
    $text = array(
    $config['client_secret'],
    'api_key' . $config['client_id'],
    'frob' . $frob,
    'method' . $method,
    'format' . $format,
    'nojsoncallback1');

    // sort array by alphabet order
    asort($text);

    // convert array to string
    $raw = '';
    foreach ($text as $s) {
    $raw.=$s;
    }
    // generate app sign with md5
    $api_sig = md5($raw);

    $endpoint = 'https://api.flickr.com/services/rest';
    $post_fields = 'method=' . 'flickr.auth.getToken' .
    '&api_key=' . $config['client_id'] .
    '&frob=' . $prog .
    '&api_sig=' . $api_sig .
    '&format=' . $format .
    '&nojsoncallback=' . $nojsoncallback;
    $header = array(
    'POST services/rest HTTP/1.1',
    'Host api.flickr.com',
    );
    $data = projecthelper::make_request($endpoint, $header, $post_fields);
  • Get result, if there is no error, save the result get into $_SESSION and navigate user to the login page.
  •  $auth = $data['auth'];
    $access_token = array();
    $access_token['access_token'] = $auth->token->_content;
    $access_token['nsid'] = $auth->user->nsid;
    $access_token['username'] = $auth->user->username;
    $access_token['fullname'] = $auth->user->fullname;

    $_SESSION[$config['session']] = $access_token;
    header("Location: " . $config['login']);
  • If there are errors, print to the screen
  •  print_r($data);

+ If there are errors, print to the screen

  • print_r($_GET);

<?php

if (!session_id()) {
session_start();
}
include 'projecthelper.php';

$config = projecthelper::get_flickr_config();
if (isset($_GET['frob'])) {
$frob = $_GET['frob'];
$method = 'flickr.auth.getToken';
$format = 'json';
$nojsoncallback = 1;
$text = array(
$config['client_secret'],
'api_key' . $config['client_id'],
'frob' . $frob,
'method' . $method,
'format' . $format,
'nojsoncallback1');

// sort array by alphabet order
asort($text);

// convert array to string
$raw = '';
foreach ($text as $s) {
$raw.=$s;
}
// generate app sign with md5
$api_sig = md5($raw);

$endpoint = 'https://api.flickr.com/services/rest';
$post_fields = 'method=' . 'flickr.auth.getToken' .
'&api_key=' . $config['client_id'] .
'&frob=' . $prog .
'&api_sig=' . $api_sig .
'&format=' . $format .
'&nojsoncallback=' . $nojsoncallback;
$header = array(
'POST services/rest HTTP/1.1',
'Host api.flickr.com',
);
$data = projecthelper::make_request($endpoint, $header, $post_fields);

if (isset($data['stat']) || $data['stat'] == 'ok') {
$auth = $data['auth'];
$access_token = array();
$access_token['access_token'] = $auth->token->_content;
$access_token['nsid'] = $auth->user->nsid;
$access_token['username'] = $auth->user->username;
$access_token['fullname'] = $auth->user->fullname;

$_SESSION[$config['session']] = $access_token;
header("Location: " . $config['login']);
} else {
print_r($data);
}
} else {
print_r($_GET);
}

After implement code, run project and view result or you can view our example here. If you have questions, don't hesitate, tell us