对于那些愿意使用我强烈推荐的 Zend_OAuth_Consumer 解决方案的人,因为它处理了处理 OAuth 请求所需的所有琐碎步骤,以及响应响应后的所有困难过程,这是我的工作代码:
1)建立消费者实例:
$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)获取请求令牌并将其存储在 $_SESSION 变量中(或使用您想要的任何其他存储机制)。确保将返回对象序列化为会话变量的字符串:
$token = $consumer->getRequestToken();
$_SESSION['FLICKR_REQUEST_TOKEN'] = serialize($token);
3)如果我在设置权限时遇到问题,请使用消费者实例的方法进行重定向,并在此时设置您的权限(即“读取”、“写入”、“删除”等):
$consumer->redirect(array("perms" => FLICKR_API_PERMS /* 'read', 'write', 'delete', etc... */));
4) 在您重定向到的页面(即 FLICKR_REDIRECT_URL)上,创建一个新的消费者实例,现在获取一个访问令牌,将其存储在 $_SESSION 中,然后取消设置会话请求令牌变量:
$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)此时您应该能够向 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();
我希望这对某人有很大帮助。代码业力 =)