facebook图api从2.2到2.3不工作 [英] facebook graph api not work from 2.2 to 2.3
问题描述
但是当我使用2.3时,我发现大多数api请求响应什么都没有,但是我找不到任何更新在升级文件中。例如:
https://graph.facebook.com/v2.3/ {$ user_id}?date_format = U& fields = albums.order(reverse_chronological).limit(100).offset(0){id,count,name,created_time}
$如果我使用2.3,b $ b
将不会返回。
当我打电话时,我无法获取用户的生日:
https://graph.facebook.com/ v2.3 / {$ user_id}
它只是返回名称和实时位置。
但是在v2.2中,它包含生日配置文件。
我使用的是Facebook SDK 3.2.2,因为我的php版本是5.3。
有没有我不知道的更新?谢谢。
我自己发现了这个问题。这是因为SDK 3.2.2。对于Facebook更新(从API版本2.3的更改日志):
[Oauth Access Token]格式 - https://www.facebook.com/v2.3/oauth/access_token 返回有效的JSON而不是URL编码。这个响应的新格式是{access_token:{TOKEN},token_type:{TYPE},expires_in:{TIME}}。我们使此更新符合RFC 6749的第5.1节。
但是,SDK将响应识别为数组(在getAccessTokenFromCode中函数):
$ response_params = array();
parse_str($ access_token_response,$ response_params);
if(!isset($ response_params ['access_token'])){
return false;
}
return $ response_params ['access_token'];
这将无法正确获取用户访问令牌,您无法获取用户的数据。所以你应该更新这个函数来解析数据json:
$ response = json_decode($ access_token_response);
if(!isset($ response-> access_token)){
return false;
}
return $ response-> access_token;
然后所有的功能将照常工作。
此外,您必须对 setExtendedAccessToken()
进行类似的更改。否则,您的应用将无法扩展访问令牌。以下代码演示如何升级功能。
/ **
*扩展访问令牌,同时删除可能通过客户端流生成
*的短命令令牌。感谢http://bit.ly/ b0Pt0H
*的解决方法。
* /
public function setExtendedAccessToken(){
try {
//直接调用_oauthRequest
//需要绕过json_decode,因为响应不是JSON格式。
$ access_token_response = $ this-> _oauthRequest(
$ this-> getUrl('graph','/ oauth / access_token'),
$ params = array(
'client_id'=> $ this-> getAppId(),
'client_secret'=> $ this-> getAppSecret(),
'grant_type'=>'fb_exchange_token',
'fb_exchange_token'=> $ this-> getAccessToken(),
)
);
}
catch(FacebookApiException $ e){
//很可能该用户最近被撤销了授权。
//无论如何,我们没有访问令牌,所以说。
返回false;
}
if(empty($ access_token_response)){
return false;
}
//版本2.2和下(已弃用)。有关更多信息,请参阅http://stackoverflow.com/a/43016312/114558
// $ response_params = array();
// parse_str($ access_token_response,$ response_params);
//
// if(!isset($ response_params ['access_token'])){
// return false;
//}
//
// $ this-> destroySession();
//
// $ this-> setPersistentData(
//'access_token',$ response_params ['access_token']
//);
//版本2.3及以上版本。
$ response = json_decode($ access_token_response);
if(!isset($ response-> access_token)){
return false;
}
$ this-> destroySession();
$ this-> setPersistentData(
'access_token',$ response-> access_token
);
}
Because it's due date for graph api 2.2, I'm trying fix my graph api using v2.3 But I discover most api request response nothing when I use 2.3, but I can not found any update for this in the upgrade document. For example:
https://graph.facebook.com/v2.3/{$user_id}?date_format=U&fields=albums.order(reverse_chronological).limit(100).offset(0){id,count,name,created_time}
will return nothing if I use 2.3. And I can't get user's birthday when I call:
https://graph.facebook.com/v2.3/{$user_id}
It's only return name and live location. But in v2.2, it include birthday profile.
I use facebook SDK 3.2.2 because my php version is 5.3. Is there any update that I don't know? Thanks.
I have found the problem myself. It's because the SDK 3.2.2. For facebook update (from the Changelog for API version 2.3):
[Oauth Access Token] Format - The response format of https://www.facebook.com/v2.3/oauth/access_token returned when you exchange a code for an access_token now return valid JSON instead of being URL encoded. The new format of this response is {"access_token": {TOKEN}, "token_type":{TYPE}, "expires_in":{TIME}}. We made this update to be compliant with section 5.1 of RFC 6749.
But SDK is recognize the response as an array(in the getAccessTokenFromCode function):
$response_params = array();
parse_str($access_token_response, $response_params);
if (!isset($response_params['access_token'])) {
return false;
}
return $response_params['access_token'];
This will not get user access token correctly, and you can't get user's data. So you should update this function to parse data as json:
$response = json_decode($access_token_response);
if (!isset($response->access_token)) {
return false;
}
return $response->access_token;
Then all of the function will work as usual.
Additionally, you must make similar changes to setExtendedAccessToken()
. Otherwise, your app won't be able to extend access tokens. The code below demonstrates how to upgrade the function.
/**
* Extend an access token, while removing the short-lived token that might
* have been generated via client-side flow. Thanks to http://bit.ly/ b0Pt0H
* for the workaround.
*/
public function setExtendedAccessToken() {
try {
// need to circumvent json_decode by calling _oauthRequest
// directly, since response isn't JSON format.
$access_token_response = $this->_oauthRequest(
$this->getUrl('graph', '/oauth/access_token'),
$params = array(
'client_id' => $this->getAppId(),
'client_secret' => $this->getAppSecret(),
'grant_type' => 'fb_exchange_token',
'fb_exchange_token' => $this->getAccessToken(),
)
);
}
catch (FacebookApiException $e) {
// most likely that user very recently revoked authorization.
// In any event, we don't have an access token, so say so.
return false;
}
if (empty($access_token_response)) {
return false;
}
//Version 2.2 and down (Deprecated). For more info, see http://stackoverflow.com/a/43016312/114558
// $response_params = array();
// parse_str($access_token_response, $response_params);
//
// if (!isset($response_params['access_token'])) {
// return false;
// }
//
// $this->destroySession();
//
// $this->setPersistentData(
// 'access_token', $response_params['access_token']
// );
//Version 2.3 and up.
$response = json_decode($access_token_response);
if (!isset($response->access_token)) {
return false;
}
$this->destroySession();
$this->setPersistentData(
'access_token', $response->access_token
);
}
这篇关于facebook图api从2.2到2.3不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!