Php 如何获取用户访问令牌?
我正在尝试通过Facebook API访问分数,以获得我朋友的分数列表。当我尝试访问Php 如何获取用户访问令牌?,php,facebook,facebook-graph-api,access-token,facebook-access-token,Php,Facebook,Facebook Graph Api,Access Token,Facebook Access Token,我正在尝试通过Facebook API访问分数,以获得我朋友的分数列表。当我尝试访问/[facebook id]/scores时,即使使用我的应用程序访问令牌,也会出现以下错误: FacebookApiException[0]:请求此资源需要用户访问令牌。~APPPATH/classes/basefacebook.php[1039] 如何获取用户访问令牌 更新:阅读后,我确认应用程序访问令牌和用户访问令牌是两个不同的访问令牌。问题仍然存在,如何获取用户访问令牌?我将此函数称为: FB.getLo
/[facebook id]/scores
时,即使使用我的应用程序访问令牌,也会出现以下错误:
FacebookApiException[0]:请求此资源需要用户访问令牌。~APPPATH/classes/basefacebook.php[1039]
如何获取用户访问令牌
更新:阅读后,我确认应用程序访问令牌和用户访问令牌是两个不同的访问令牌。问题仍然存在,如何获取用户访问令牌?我将此函数称为:
FB.getLoginStatus(onFacebookInitialLoginStatus);
它调用下面的函数并从响应中获取access_令牌
function onFacebookInitialLoginStatus(response) {
if (response.status == "connected" && response.session) {
var access_token = response.session.access_token;
var url = 'https://graph.facebook.com/me/likes?access_token=' + access_token;
}
else {
facebookLogin();
}
}
我看到您正在使用PHPSDK。因此,您需要您的朋友使用以下脚本访问您的应用程序:
php sdk
身体{
字体系列:“Lucida Grande”,Verdana,Arial,无衬线;
}
H1A{
文字装饰:无;
颜色:#3b5998;
}
H1A:悬停{
文字装饰:下划线;
}
php sdk
使用PHP SDK处理的OAuth 2.0登录:
PHP会话
你
/图片“>
您的用户对象(/me)
您没有连接。
奈提克的公众简介
然后你需要:
脱机访问权限
access\u令牌
$facebook->api('friend\u id/scores?access\u token=$stored\u access\u token');
请参阅显示Facebook身份验证流程的文档 这是开始了解它是如何工作的最好地方
请勿将访问令牌误认为您的应用程序密码,该密码用于授权代表您注册的应用程序建立连接。请参阅此事件:
然后检查response.status//谢谢你。我添加了“脱机范围”,但如何访问令牌?我使用了$Facebook->getAccessToken(),但它不起作用。$Facebook是PHP中Facebook sdk的一个实例。你需要将
脱机访问
添加到你的范围中,然后一旦你的朋友连接到你的应用程序(在if($user)
内)调用您提到的函数将返回您需要存储的访问令牌!//谢谢!!它现在可以工作了。我认为需要“脱机访问”才能使其工作。脱机访问权限已被弃用,不久将被删除。当出现此线程时,它仍然有效,但即使在那时,您也不需要它。永远不需要来自该线程的更多权限抱歉,这是一个非常糟糕的答案,而且在第3步中php sdk的用法也不正确……这不是您实际的应用程序机密,是吗?如果是,请确保更改应用程序机密。控制台中显示onFacebookInitialLoginStatus
未定义
<?php
/**
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '191149314281714',
'secret' => '73b67bf1c825fa47efae70a46c18906b',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'offline_access'));
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
FacebookDesktop.getSession().accessToken;
FB.Event.subscribe('auth.authResponseChange', function(response) {