use*_*183 57 php api facebook access-token
我一直在努力找出这个发生了什么.我的脚本工作正常,突然一半停止了.
我正在访问api并且正在获取访问令牌.使用访问令牌,我可以很好地访问用户的公共信息.但是,当我尝试将信息发布到他们的FB帐户时,我收到此错误.
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
Run Code Online (Sandbox Code Playgroud)
知道这里发生了什么吗?我也在我的网站上使用会话来跟踪内部用户ID.不确定我的会话是否可能导致问题.
这是我上传的脚本,我收到了一个错误.
require 'facebook/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '12345678',
'secret' => 'REMOVED',
'fileUpload' => true,
'cookie' => true,
));
$facebook->setFileUploadSupport(true);
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
$upload_photo = $facebook->api('/me/photos', 'post', $photo_details);
Run Code Online (Sandbox Code Playgroud)
yac*_*con 32
$facebook->api('/'.$facebook_uid)
Run Code Online (Sandbox Code Playgroud)
代替
$facebook->api('/me')
Run Code Online (Sandbox Code Playgroud)
有用.
ifa*_*our 31
只需检查当前的Facebook用户ID $user,如果它返回null,则需要重新授权用户(或使用自定义$_SESSION用户ID值 - 不推荐)
require 'facebook/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$user = $facebook->getUser();
$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
if ($user) {
try {
// We have a valid FB session, so we can use 'me'
$upload_photo = $facebook->api('/me/photos', 'post', $photo_details);
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
// redirect to Facebook login to get a fresh user access_token
$loginUrl = $facebook->getLoginUrl();
header('Location: ' . $loginUrl);
}
Run Code Online (Sandbox Code Playgroud)
我写了一个关于如何将图片上传到用户墙上的教程.
Arj*_*ran 18
所以我有同样的问题,但这是因为我保存了访问令牌但没有使用它.可能是因为我因为截止日期而非常困,或者我只是没想到它!但万一其他人处于同样的情况:
当我登录用户时,我保存了访问令牌:
$facebook = new Facebook(array(
'appId' => <insert the app id you get from facebook here>,
'secret' => <insert the app secret you get from facebook here>
));
$accessToken = $facebook->getAccessToken();
//save the access token for later
Run Code Online (Sandbox Code Playgroud)
现在当我向facebook发出请求时,我只是做了这样的事情:
$facebook = new Facebook(array(
'appId' => <insert the app id you get from facebook here>,
'secret' => <insert the app secret you get from facebook here>
));
$facebook->setAccessToken($accessToken);
$facebook->api(... insert own code here ...)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
161164 次 |
| 最近记录: |