使用 curl 查询图形 API



我想检查如何处理access_token。因此,我使用 curl 来执行以下查询:

curl -X POST https://graph.facebook.com/oauth/access_token -d "client_id=<appId>&client_secret=<secret>&grant_type=client_credentials&redirect_uri="

它返回一个值 access_token

然后,我想获取我的朋友列表:

curl -X POST https://graph.facebook.com/me/friends -d "access_token=<token>"

它返回此错误:

{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}

有什么提示吗?

简单的方法是使用图形 API 资源管理器,转到那里并使用您的 facebook 开发人员帐户获取和访问令牌:

生成基本用户访问令牌

当您开始构建自己的应用程序时,您需要了解 访问令牌以及如何使用 Facebook 登录生成它们,但 现在,我们可以通过图形 API 资源管理器快速获取一个:

Click on the Get Token button in the top right of the Explorer.
Choose the option Get User Access Token.
In the following dialog don't check any boxes, just click the blue Get Access Token button.
You'll see a Facebook Login Dialog, click **OK" here to proceed.

您可以将图形 api 资源管理器用作 curl,但如果您想尝试使用真正的 curl,则最后一个 api v2.8 的 sintaxis 如下:

curl -i -H 'Authorization: Bearer YOUR_ACCES_TOKEN' -XGET 'https://graph.facebook.com/v2.8/me'

就我而言:

toni@MBP-de-Antonio  ~  curl -i -H 'Authorization: Bearer MY-ACCES_TOKEN' -XGET 'https://graph.facebook.com/v2.8/me'
HTTP/1.1 200 OK
x-app-usage: {"call_count":2,"total_cputime":3,"total_time":3}
Expires: Sat, 01 Jan 2000 00:00:00 GMT
x-fb-trace-id: BnLv25AHTjq
facebook-api-version: v2.8
Content-Type: application/json; charset=UTF-8
x-fb-rev: 2929740
Cache-Control: private, no-cache, no-store, must-revalidate
Pragma: no-cache
ETag: "39875e94193dcd62dcbbf583fc0008c110820a6c"
Access-Control-Allow-Origin: *
X-FB-Debug: fvO9W8Bfl+BihEy/3aZyzOiMrXOkrbK8q1I3Xk2wYnI7sSujZNC6vQzR4RoTWK7K3Hx6EdzoE2kZ/aWhsXe4OA==
Date: Sat, 01 Apr 2017 06:55:52 GMT
Connection: keep-alive
Content-Length: 61
{"name":"Antonio Juan Querol Giner","id":"10204458008519686"}%

你试过使用调试器吗?在此处调试访问令牌,它会告诉您该访问令牌,以及它是否与用户相关联:https://developers.facebook.com/tools/debug

另外,你为什么要使用POST来获取/me/friends?

我认为

您的问题可能是您尝试访问自己的数据时使用的"/me"标识符。(根据您的客户端 ID 设置的内容,因为您可能指的是应用程序而不是您自己,请尝试使用您的个人资料 ID 而不是"/me")。

另请查看此 https://stackoverflow.com/a/6701982/1546542

您应该将"https://...."括起来,并在curl -X POST后加上双引号。

最新更新