Home » Php » php – Using Zend_OAuth_Consumer to authorize with Flickr API

php – Using Zend_OAuth_Consumer to authorize with Flickr API

Posted by: admin July 12, 2020 Leave a comment

Questions:

For those willing to use the Zend_OAuth_Consumer solution, which I highly recommend, as it handles all the trivial steps needed to process an OAuth request, and all the difficult procedures that follow the response, here is my working code:

1) Establish the consumer instance:

$consumer = new Zend_Oauth_Consumer(array(
    'callbackUrl' => FLICKR_REDIRECT_URL /* from my app settings */,
    'siteUrl' => 'http://www.flickr.com/services/oauth',
    'consumerKey' => FLICKR_API_KEY,
    'consumerSecret' => FLICKR_API_SECRET,
    'requestTokenUrl' => FLICKR_REQUEST_TOKEN_URL /* www.flickr.com/services/oauth/request_token */,
    'accessTokenUrl' => FLICKR_ACCESS_TOKEN_URL /* www.flickr.com/services/oauth/access_token */,
    'authorizeUrl' => FLICKR_AUTHORIZE_URL /* www.flickr.com/services/oauth/authorize */
));

2) Get the request token and store it in a $_SESSION variable (or use whatever other storage mechanism you want). Be sure to serialize the return object as a string for the session variable:

$token = $consumer->getRequestToken();

$_SESSION['FLICKR_REQUEST_TOKEN'] = serialize($token);

3) Where I had issues setting the permissions, use the consumer instance’s method to redirect, and set your perms at this point (i.e. ‘read’, ‘write’, ‘delete’, etc…):

$consumer->redirect(array("perms" => FLICKR_API_PERMS /* 'read', 'write', 'delete', etc... */));

4) On the page that you redirected to (i.e. FLICKR_REDIRECT_URL), create a new consumer instance, and now grab an access token, store it in the $_SESSION, and then unset the session request token variable:

$consumer = new Zend_Oauth_Consumer(array(
    'callbackUrl' => FLICKR_REDIRECT_URL /* from my app settings */,
    'siteUrl' => 'http://www.flickr.com/services/oauth',
    'consumerKey' => FLICKR_API_KEY,
    'consumerSecret' => FLICKR_API_SECRET,
    'requestTokenUrl' => FLICKR_REQUEST_TOKEN_URL /* www.flickr.com/services/oauth/request_token */,
    'accessTokenUrl' => FLICKR_ACCESS_TOKEN_URL /* www.flickr.com/services/oauth/access_token */,
    'authorizeUrl' => FLICKR_AUTHORIZE_URL /* www.flickr.com/services/oauth/authorize */
));

$token = $consumer->getAccessToken($_GET, unserialize($_SESSION['FLICKR_REQUEST_TOKEN']));

$_SESSION['FLICKR_ACCESS_TOKEN'] = serialize($token);

$_SESSION['FLICKR_REQUEST_TOKEN'] = null;

5) At this point you should be able to make authorized requests to Flickr:

$token = unserialize($_SESSION['FLICKR_ACCESS_TOKEN']);
$client = $token->getHttpClient(array(
    'callbackUrl' => FLICKR_REDIRECT_URL /* from my app settings */,
    'siteUrl' => 'http://www.flickr.com/services/oauth',
    'consumerKey' => FLICKR_API_KEY,
    'consumerSecret' => FLICKR_API_SECRET,
    'requestTokenUrl' => FLICKR_REQUEST_TOKEN_URL /* www.flickr.com/services/oauth/request_token */,
    'accessTokenUrl' => FLICKR_ACCESS_TOKEN_URL /* www.flickr.com/services/oauth/access_token */,
    'authorizeUrl' => FLICKR_AUTHORIZE_URL /* www.flickr.com/services/oauth/authorize */
));
$client->setUri("http://api.flickr.com/services/rest/");
$client->setMethod(Zend_Http_Client::GET);
$client->setParameterGet("method", "flickr.contacts.getList");
$client->setParameterGet("api_key", FLICKR_API_KEY);
$response = $client->request();

header('Content-Type: text/xml');
echo $response->getBody();

I hope this helps someone, greatly. Code karma =)

How to&Answers: