gpt4 book ai didi

php - 必须使用事件访问 token 来查询有关当前用户的信息 |访问 token 似乎有效

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

我已经定义了一个访问 token 并拥有“喜欢”Facebook 页面所需的权限,但 Firebug 一直在标题中给我错误。 请不要将此帖子标记为重复,因为我已经查看了有关该问题的帖子,但找不到以任何方式适合我的特定情况的答案。 我不明白为什么或我应该如何解决它。我的代码如下:

utils.php

    <?php
require_once('sdk/src/facebook.php');
require_once("AppInfo.php");
/**
* @return the value at $index in $array or $default if $index is not set.
*/
function idx(array $array, $key, $default = null) {
return array_key_exists($key, $array) ? $array[$key] : $default;
}

function he($str) {
return htmlentities($str, ENT_QUOTES, "UTF-8");
}
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'sharedSession' => true,
'trustForwarded' => true,
'file_upload' =>true
));
$user_id = $facebook->getUser();
if($user_id)
{
$logoutUrl =$facebook->getLogoutUrl();
}
else
{
$loginUrl=$facebook->getLoginUrl();
}
if ($user_id) {
try {
// Fetch the viewer's basic information
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
// If the call fails we check if we still have a user. The user will be
// cleared if the error is because of an invalid accesstoken
if (!$facebook->getUser()) {
header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
exit();
}
}
}
?>

likes.php

      <?php

require_once("sdk/src/facebook.php");
require_once("utils.php");
require_once("AppInfo.php");
$permissions = $facebook->api('/me/permissions ');
if( array_key_exists('publish_actions', $permissions['data'][0]) ) {
// Permission is granted!
// Do the related task
//$post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!'));
} else {
// We don't have the permission
// Alert the user or ask for the permission!
header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_actions")) );
}
?>
<!DOCTYPE html>
<html xmlns:fb="http://ogp.me/ns/fb#">
<head>

<style type="text/css">
li{
vertical-align: middle;
padding-top: 1em;
}
</style>
<div id="fb-root"></div>
<script type="text/javascript" src="/javascript/jquery-1.7.1.min.js"></script>
<script type="text/javascript">

(function(d, debug){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, false)); //loads javascript-sdk

$(function() {
// Set up so we handle click on the buttons
$('#like_but').click(function(){
FB.api(
'me/og.likes',
'post',
{
object: "http://facebook.com/216730015024772"
},
function(response) {
alert(response);
});});}); // they are closed properly, don't bother checking it. (!) Should like the 'object'
</script>
</head>
<body>
<div style="position:fixed; display:block">
<input type="button" value="Like" id="like_but"/>
</div>
</body>
</html>

有谁知道为什么会出现错误或我该如何解决这个问题?任何提示将不胜感激。

注意:用户是从另外一个index.php登录的,这里就不贴了,因为没有问题,access token还是聚集在utils.php中。此外,当检查是否在“likes.php”中授予权限时,它工作正常。

最佳答案

假设您从 Facebook 收到的 access_token 确实有效...看起来您对 Facebook javascript SDK 的实现并不完整。

您已经包含了库加载程序,但是您没有用于初始化库的 FB.init(); 部分。我在第二个代码块中没有看到对任何 appId 的引用,因此 Facebook 有办法知道您的代码引用了哪个应用。

请引用following documentation from Facebook .

具体来说,这可能会解决您的问题:

window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'YOUR_APP_ID', // App ID from the App Dashboard
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
xfbml : true // parse XFBML tags on this page?
});

// Additional initialization code such as adding Event Listeners goes here
document.getElementById('like_but').addEventListener('click', function() {
FB.api('me/og.likes', 'post', {
object: "http://facebook.com/216730015024772"
}, function(response) {
console.log(response);
});
}, false);
};

(function(d, debug){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));

关于php - 必须使用事件访问 token 来查询有关当前用户的信息 |访问 token 似乎有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15281291/

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