0

我想要一个这样的 URL用于Facebook 图形 API 搜索来检索数据。

当我尝试使用这个url时,它给出了

{
   "error": {
      "message": "(#200) Must have a valid access_token to access this endpoint",
      "type": "OAuthException",
      "code": 200
   }
}

我将在 Android 上使用它。有人可以指导我实现这一目标。

4

1 回答 1

2

Facebook's Graph API was public until recently when they added this access_token.

From their documentation they explain how to create a temporary access token. Unfortunately, creating in this way, you will need to deal with some sort of job that will update (i.e. renew existing access token/generate a new access token) once in a while your access_token so you can be able to make use of the Graph API.

Most probably, there are some libraries out there that might deal with renewing or generating a new access token, but maybe it won't fit with your app.

So to avoid the previously mentioned "problem", I found there is a way to have a non-expiring access token.

What you will need to do is the following steps:

  1. Create a Facebook account (if you don't have one or if you want to create an account just for development purposes)
  2. Go at Facebook Developers page (Register as developer, activate account and whatnot)
  3. Create a new app on the Facebook Developers App page
  4. Go to your app and at your Settings, click on Edit Settings.
  5. Go at Sandbox Mode and select Disabled
  6. On the left hand side, from the Settings section, click on Advanced.
  7. At the Authentication section, check App Type and select Web (if it is not already that)
  8. For the final step, go back to the Basic section in Settings and you will see two app specifics:

    App ID and App Secret

    You can make use of these two codes in order to benefit from a access_token which will not expire as follows: APP ID|APP Secret (observe the vertical bar between the codes)

So now you can make requests like so:

https://graph.facebook.com/search?q=mark&access_token=APP_ID|APP_SECRET

Observe that I did not mentioned in the url the type=user. For your case, you will need to create expiring access tokens. You can quickly create a temporary token like this:

GET /oauth/access_token?
 client_id={app-id}
  &client_secret={app-secret}
  &grant_type=client_credentials

(See Access Tokens page from Facebook's documentation). If you cannot find a solution that will deal automatically the refreshing of the access token, I can explain what I did by creating background job that will update it once in a while:

This will return a response similar to this:

access_token=NEW_ACCESS_TOKEN&expires=TIME_UNTIL_IT_EXPIRES.

As far as I remember, you will be able to use the newly create access token for 60 days with no problem (It was easier for me to generate a new one once a month making use of cron jobs).

So, having that response you can quickly do a simple regex:

access_token=(.*)&expires=(.*)

And you can do:

String newAccessToken = matcher.group(1);

Replace the existing access token in your file/db table and you're done.

With this access token you will be able to make your request:

https://graph.facebook.com/search?q=mark&type=user&access_token=APP_ID|APP_SECRET
于 2013-07-30T08:29:39.283 回答