简体   繁体   English

Facebook PHP SDK使用php获取访问令牌

[英]Facebook PHP SDK get access token using php

I am using the following code to post to Facebook: 我使用以下代码发布到Facebook:

require('facebook.php');

$fb = new Facebook(array('appId' => 'MY APP ID','secret' => 'MY APP SECRET','cookie' => true));

$result = false;
$feed_dir = '/401868882779/feed/';  //to the UID you want to send to
$acToken = "MY ACCESS TOKEN";
$url = 'URL';
$link = $url . 'event.php?id=' . $id;
if (isset($picture))
{
    $picture = $url . 'uploads/' . $picture;
}
else
{
    $picture = $url . 'images/blank100x70.png';
}
$msg_body = array('access_token' => $acToken,'name' => $noe_unsecured,'message' => $link,'link' => $link,'description' => $description_unsecured,'picture' => $picture);

try
{
    $result = $fb->api($feed_dir, 'post', $msg_body);
} 
catch (Exception $e)
{     
        $err_str = $e->getMessage();
}

but I need to update the access token manually every time it changes. 但我需要在每次更改时手动更新访问令牌。 I am sure there's solution but I cant find it.. I tried lots of scripts and nothing worked. 我确信有解决方案,但我找不到它..我尝试了很多脚本,没有任何效果。

Depending on when you perform the wall post, you might need to request the offline_access permission. 根据您执行墙贴的时间,您可能需要请求offline_access权限。 This will convert your access_token into a format that does not expire so there would be no need to refresh the token. 这会将您的access_token转换为不会过期的格式,因此无需刷新令牌。

A simple solution... remove the access_token ! 一个简单的解决方案...... 删除 access_token
You simply don't need it as long as you got the publish_stream permission! 只要你获得了publish_stream权限,你根本就不需要它!

I believe there are multiple methods to do this: 我相信有多种方法可以做到这一点:

- you can use the method already provided in the PHP SDK getAccessToken which returns the current access token being used by the sdk instance, more info at this url . - 您可以使用PHP SDK getAccessToken提供的方法,该方法返回sdk实例使用的当前访问令牌, 此URL上有更多信息。

- However you need not use an access token to call the api() method, once you ask the user for the publish_stream permission, as already mentioned by @ifaour. - 但是,一旦您向用户请求publish_stream权限,您就不需要使用访问令牌来调用api()方法,如@ifaour已经提到的那样。 Hence you can do something like this example , scroll to the subheading Post a link to a User's wall using the Graph API . 因此,您可以执行类似此示例的操作 ,滚动到子标题使用Graph API发布指向用户墙的链接

- Then, you have another three options - 然后,你有另外三个选择

i) either get a new access token using the method here , if you are posting when the user is currently using your app, otherwise you can try the next 2 options i)要么使用此处的方法获取新的访问令牌,如果您在用户当前正在使用您的应用程序时发布,否则您可以尝试接下来的2个选项
ii) get offline access ii) 离线访问
iii) i'm not sure of this, but you might want to try with an app access token which can be obtained this way . iii)我不确定这一点,但你可能想尝试使用这种方式获得的app访问令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM