Using Zend_OAuth_Consumer to authorize with Flickr API [closed]
Asked Answered
R

0

8

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 =)

Ray answered 25/7, 2012 at 19:15 Comment(2)
Hey, thanks for posting this Chris! You should consider splitting this into a discrete question and answer, so that folks can vote on the answer itself... See: blog.stackoverflow.com/2011/07/…Repository
You might want to take a look at Guzzle aswell in the future :-) it has an plugin for Oauth and it works fab-u-lous!Unsettled

© 2022 - 2024 — McMap. All rights reserved.