如何通过 Facebook PHP SDK 获取应用访问令牌?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18715817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:05:05  来源:igfitidea点击:

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

phpfacebookfacebook-graph-apifacebook-php-sdkfacebook-access-token

提问by Boehmi

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. I did nearly the same thing in C# and there it works.

我还用完整的 oauth/accesstoken 链接替换了第一部分,但随后它返回了我不想要的有关 oauth 及其 Facebook 页面的一般信息。我在 C# 中做了几乎相同的事情,并且在那里工作。

回答by Lix

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 |and the application secret.

那是应用程序的 id、管道字符|和应用程序机密。

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.

确保您的用户无法访问此应用程序令牌!仅在服务器端使用和引用它 - 永远不要将其传递给客户端。有关更多信息,请查看相关文档中的最后几句话。

回答by tremby

With version 5 of the SDK you can get the access token with the accessToken()method of a FacebookAppinstance. If you have a Facebookinstance (as you normally would) you can get it like this:

使用 SDK 的第 5 版,您可以accessToken()使用FacebookApp实例的方法获取访问令牌。如果你有一个Facebook实例(就像你通常那样),你可以像这样得到它:

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

When I want to use my own app's access token I'm instantiating the API like this. I don't know if there's a cleaner way.

当我想使用我自己的应用程序的访问令牌时,我正在像这样实例化 API。不知道有没有更干净的方法。

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

回答by Sahil Mittal

Replace -

代替 -

'&client_secret' => 'client_secret'

'&grant_type' => 'grant_type'