使用 graph api 获取照片
我在这里阅读了很多关于此的教程/文章/问题,并尝试在 fb 文档中找到有用的内容。
到目前为止,我自己还没有取得任何进展,因此任何输入都将不胜感激,我只是想访问我的照片列表,但我得到的只是一个空数组。
我知道我添加的 req_perms 可能比我需要的多,我只是从“工作教程”中复制了那些对我不起作用的内容,在阅读此处的线程后,我还添加了 user_photo_video_tags 因为这对线程海报有效(再次) ,不是我)。
我已经收到允许照片与我的应用程序共享照片的对话框,登录没有任何问题,我获得的访问令牌似乎是正确的,登录后我访问了:
https://graph.facebook.com/me/photos?access_token= 和令牌,并得到一个空数组,如果我没有登录或 access_token没有链接到我的应用程序会有一些错误,但我得到的只是一个空数组。
预先感谢您的任何意见。
感谢 Chaney Blu 我能够验证我的权限:
{
"data": [
{
"installed": 1,
"status_update": 1,
"photo_upload": 1,
"video_upload": 1,
"create_note": 1,
"share_item": 1,
"publish_stream": 1
}
]
}
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once 'library/facebook.php';
$app_id = "xxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$loginLink = $facebook->getLoginUrl(array(
'scope' => 'user_status,publish_stream,user_photos,user_photo_video_tags'
));
$logOutLink = $facebook->getLogoutUrl();
$user = $facebook->getUser();
if ($user) {
try {
// User logged in, get token
$token = $facebook->getAccessToken();
//var_dump($token); dumped successfully
// Get public profile info
$user_profile = $facebook->api('/me');
//var_dump($user_profile); dumped successfully
$photos = $facebook->api('/me/photos?access_token=' . $token);
var_dump($photos); // Empty array, BAH!
} catch (FacebookApiException $e) {
$user = null;
}
}
?>
<a href="<?php echo $loginLink; ?>">Click here to login if you aren't autoredirected</a><br /><br /><br />
<a href="<?php echo $loginLink; ?>">Click here to logout</a>
I've read a lot of tutorials/articles/questions here about this, as well as trying to find something useful in the fb documentation.
So far I've made no progress at all myself so any input would be greatly appreciated, I'm simply trying to access a list of my photos but all I get is an empty array.
I know I've added more req_perms than I need probably, I just copied the ones from a "working tutorial" that didnt work for me, and after reading a thread here I also added user_photo_video_tags because that had worked for the thread poster (again, not me).
I've gotten the dialog to allow photos sharing my photos with my app, login works without any problems, the access token I get seem to be correct, after logging in I have visited:
https://graph.facebook.com/me/photos?access_token= and the token, and gotten an empty array, if I wasnt logged in or the access_token wasnt linked to my app there would be some error, but all I get is an empty array.
Thanks in advance for any input.
Thanks to Chaney Blu I was able to validate my permissions:
{
"data": [
{
"installed": 1,
"status_update": 1,
"photo_upload": 1,
"video_upload": 1,
"create_note": 1,
"share_item": 1,
"publish_stream": 1
}
]
}
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once 'library/facebook.php';
$app_id = "xxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$loginLink = $facebook->getLoginUrl(array(
'scope' => 'user_status,publish_stream,user_photos,user_photo_video_tags'
));
$logOutLink = $facebook->getLogoutUrl();
$user = $facebook->getUser();
if ($user) {
try {
// User logged in, get token
$token = $facebook->getAccessToken();
//var_dump($token); dumped successfully
// Get public profile info
$user_profile = $facebook->api('/me');
//var_dump($user_profile); dumped successfully
$photos = $facebook->api('/me/photos?access_token=' . $token);
var_dump($photos); // Empty array, BAH!
} catch (FacebookApiException $e) {
$user = null;
}
}
?>
<a href="<?php echo $loginLink; ?>">Click here to login if you aren't autoredirected</a><br /><br /><br />
<a href="<?php echo $loginLink; ?>">Click here to logout</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否是问题所在,但尝试一下。您似乎正在使用最新的 PHP SDK。在你的 getLoginUrl();调用,尝试将“req_perms”更改为“scope”。
像这样:
您可以通过访问 https:// 来验证您是否已授权正确的权限graph.facebook.com/me/permissions/?access_token=XXXX
Not sure if this is the problem, but try this. It appears you're using the latest PHP SDK. In your getLoginUrl(); calls, try changing 'req_perms' to 'scope'.
Like this:
You can verify that you've authorized the correct permissions by visiting https://graph.facebook.com/me/permissions/?access_token=XXXX
在测试了一些其他权限后,我注意到 Facebook 没有更新我的令牌的权限,即使在注销应用程序、再次登录并接受新权限时,当我查看从 Chaney Blu 获得的 Graph 权限链接时,也没有任何变化。
我使用该链接来验证 facebooks graph api 页面 http://developers.facebook.com 的令牌/docs/reference/api/ 并注意到令牌可以访问 user_photos 但不能访问我的令牌。
进入我的 Facebook 设置并删除该应用程序,使 Facebook 在我下次登录该应用程序时更新了我的权限。
感谢 Chaney Blu 让我走上了正确的道路。如果我有声誉的话,我会投票给你。
After testing some other permissions I noticed facebook weren't updating the permissions to my token, even when logging out of the app, logging in again and accepting new permissions nothing changed when I looked at the Graph permissions link I got from Chaney Blu.
I used that link to verify the token from facebooks graph api page http://developers.facebook.com/docs/reference/api/ and noticed that token had access to user_photos but not my token.
Going into my facebook settings and removing the app made facebook update my permissions the next time I signed into the app.
Thanks to Chaney Blu for putting me on the right track. Would vote you up if I had the reputation.