简体   繁体   English

如何使用Facebook App访问令牌?

[英]How to work with Facebook App Access Tokens?

I've been reading a lot in the last few days, and I'm sure that I've been learning a lot. 最近几天我一直在阅读很多东西,而且我相信我已经学到了很多东西。 All this due Facebook documentation being spread (in the bad sense) all over the web, making people looking for the information and trying a lot of things. 所有这些归因于Facebook的文档(在不好的意义上)遍布网络,使人们寻找信息并尝试了很多事情。

I've done some stuff like: 我已经做了一些类似的事情:

  • Posting to pages' wall (images, links, text) 发布到页面的墙上(图像,链接,文本)
  • Posting to profiles' wall 发布到个人资料墙

So, I've thought about creating a kind of an automatic poster to pages. 因此,我考虑过要创建一种自动的页面海报。 But, imagine what. 但是,想象一下。 We need Access Tokens to that, those that won't expire. 我们需要访问令牌,这些令牌不会过期。

Ok, no problem. 好,没问题 From what I've been reading App Access Tokens take care of that, and then I can run a cron with the script! 从我一直在阅读的App Access Token来看,这很重要,然后我可以使用该脚本运行cron!

But how it is supposed to run the posting with the Access Token? 但是,应该如何使用访问令牌运行发布?

Currently, with the user token, I was using this code if the id of the logged in user could be retrieved. 当前,如果可以检索到已登录用户的ID,则使用用户令牌时,我正在使用此代码。

FB.api('/<?=$album_id?>/photos?access_token=<?=$fanpage_token?>', 'post', {
    message: ementa,
    url: selected_image
    }, function(response) {
  if (!response || response.error) {
    alert('Falhou a publicar a ementa com imagem');
  } else {
    alert('Page Post ID: ' + response.id);
  }
});

I've got the fan page token manually from the Graph API Explorer, because this is just going to be used by the cronjob. 我从Graph API Explorer中手动获得了粉丝页面令牌,因为这将由cronjob使用。

Album ID i've got previously too. 我以前也有专辑ID。

How could I use this with an App Token? 如何将其与应用令牌一起使用?

Thanks! 谢谢!

First of all, you can get extended user token (that lasts for 60 days) in php 首先,您可以在php中获得扩展的用户令牌(持续60天)

$facebook = new Facebook(array(
            'appId' => APP_ID,
            'secret' => APP_SECRET,
            'fileUpload' => true
        ));     
$facebook->setExtendedAccessToken();

Then $facebook->getAccessToken() and store it to Database; 然后$facebook->getAccessToken()并将其存储到数据库;

After that, if you wish to post something to the appropriate users wall, create $facebook object, set existing access token and do the things... 之后,如果您希望将某些内容发布到适当的用户墙上,请创建$ facebook对象,设置现有的访问令牌并执行操作。

$facebook = new Facebook(array(
        'appId' => $appId,
        'secret' => $appSecret,
        'fileUpload' => true
     ));
//set token 
    $facebook->setAccessToken($fbAccessToken);
    $facebook->api('/me/photos','post', $data);

This will autonomously work with a cron job. 这将自动执行cron作业。

After long days of research on Facebook Application API usage, I've finally discovered thanks to cbaclig that Applications can't control some stuff, and do just have permissions to do stuff like Check Page Insights, Making Real-Time Updates, and some stuff like that. 经过对Facebook Application API使用情况的漫长研究之后,由于cbaclig ,我终于发现,Applications无法控制某些内容,并且仅具有执行诸如Check Page Insights,进行实时更新等内容的权限。像那样。

I discovered this in this link . 我在此链接中发现了这一点

Quoting the answer in the link (just in case that it goes offline): 在链接中引用答案(以防脱机):

It seems like the search method requires an actual user access token, not just an application access token like you're using in your example. 似乎搜索方法需要实际的用户访问令牌,而不仅仅是示例中使用的应用程序访问令牌。 I tried the same query with a logged-in user's access token, and it worked fine. 我使用登录用户的访问令牌尝试了相同的查询,并且工作正常。 The first examples on https://github.com/arsduo/koala/wiki/OAuth describe different ways of getting a user's access token (via OAuth redirects, the Javascript SDK, etc.) which you should be able to use to make your queries. https://github.com/arsduo/koala/wiki/OAuth上的第一个示例描述了获取用户访问令牌(通过OAuth重定向,Javascript SDK等)的不同方法,您应该可以使用这些方法来制作自己的查询。

In general, the app access token is only used for application-specific things, like getting your app's insight data, registering real-time updates, generating test user's, etc. 通常,应用程序访问令牌仅用于特定于应用程序的事物,例如获取应用程序的洞察数据,注册实时更新,生成测试用户的令牌等。

Let us know if that helps! 让我们知道是否有帮助!

EDIT 11/11/12: I've finally discovered that page access tokens could have access to publish_stream and manage_pages, that would allow them to publish with an expiry-never access token. 编辑11/11/12:我终于发现页面访问令牌可以访问publish_stream和manage_pages,这将使它们可以使用永不过期的访问令牌进行发布。

You need to get a Fan Page access token using an Extended Access Token of the user. 您需要使用用户的扩展访问令牌来获取粉丝页面访问令牌。 That one will never expire and you could then use it to make requests as the page forever. 那将永远不会过期,然后您可以使用它作为页面永久发出请求。

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

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