简体   繁体   English

如何通过Facebook PHP SDK获取App Access Token?

[英]How do I get the App Access Token via the Facebook PHP SDK?

I'm trying to retrieve the app access token of my app in order to post notifications, but for some reason, it doesn't work. 我正在尝试检索我的应用程序的应用程序访问令牌以发布通知,但由于某种原因,它不起作用。 Here's the code: 这是代码:

$AppParams = array(
    'client_id'     => 'myclientid',
    '&client_secret' => 'myclientsecret',
    '&grant_type'    =>'client_credentials'
    );
$AppToken = $facebook->api('oauth/access_token?', 'GET', $AppParams);

I also replaced the first part with the full oauth/accesstoken link, but then it returns me general information about oauth and their facebook page, which I do not want. 我还用完整的oauth / accesstoken链接替换了第一部分,但随后它返回了关于oauth及其facebook页面的一般信息,这是我不想要的。 I did nearly the same thing in C# and there it works. 我在C#中做了几乎相同的事情并且在那里工作。

You don't really have to request an application access token. 您实际上不必请求应用程序访问令牌。 You can simply assemble one yourself. 你可以自己组装一个。

An application access token is formatted like this: 应用程序访问令牌的格式如下:

app_id|app_secret

That's the application's id, a pipe character | 这是应用程序的ID,一个管道字符| and the application secret. 和申请秘密。

Make sure that this app token is not accessible to your users! 确保您的用户无法访问此应用令牌! Only use and reference it on the serverside - never pass this to the client. 仅在服务器端使用和引用它 - 永远不会将其传递给客户端。 For more info, check out the last few sentences in the relevant documentation . 有关详细信息,请查看相关文档中的最后几句话。

With version 5 of the SDK you can get the access token with the accessToken() method of a FacebookApp instance. 使用SDK的第5版,您可以使用FacebookApp实例的accessToken()方法获取访问令牌。 If you have a Facebook instance (as you normally would) you can get it like this: 如果你有一个Facebook实例(你通常会这样),你可以这样得到它:

$fb->getApp()->getAccessToken()

When I want to use my own app's access token I'm instantiating the API like this. 当我想使用我自己的应用程序的访问令牌时,我正在实例化这样的API。 I don't know if there's a cleaner way. 我不知道是否有更干净的方式。

$fb = new \Facebook\Facebook([
    'default_graph_version' => 'v2.8',
]);
$fb->setDefaultAccessToken($fb->getApp()->getAccessToken());

Replace - 替换 -

'&client_secret' => 'client_secret'

'&grant_type' => 'grant_type'

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

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