gpt4 book ai didi

php - 通过 Graph API 检索用户配置文件时出现 Facebook PHP SDK 访问 token 问题

转载 作者:行者123 更新时间:2023-12-04 22:19:26 25 4
gpt4 key购买 nike

我试图在我的网站上检索用户个人资料。但是拿到access token后,就不能用了。它显示的结果是

Graph returned an error: Invalid OAuth access token

我可以通过下面的代码获取访问 token

登录.php

session_start();

require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
'app_id' => 'I have insert correct app id',
'app_secret' => 'and app secret',
'default_graph_version' => 'v2.0',
]);

$helper = $fb->getRedirectLoginHelper();
$permissions = ['email'];
$loginUrl = $helper->getLoginUrl('http://myweb.com/login-callback.php', $permissions);

echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
?>

下面是login-callback.php

<?php

session_start();

require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
'app_id' => 'I have insert correct app id',
'app_secret' => 'and app secret',
'default_graph_version' => 'v2.0',
]);

$helper = $fb->getRedirectLoginHelper();

try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}

if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}

$_SESSION['fb_access_token'] = (string) $accessToken;

try {
$response = $fb->get('/me?fields=id,name', "'".$_SESSION['fb_access_token']."'");
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}

$user = $response->getGraphUser();

echo 'Name: ' . $user['name'];
?>

我做错了什么吗?请帮忙。谢谢。

最佳答案

我只有:

$response = $fb->get('/me?fields=id,name', $token);
$user = $response->getGraphUser();

完美运行

关于php - 通过 Graph API 检索用户配置文件时出现 Facebook PHP SDK 访问 token 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32738697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com