我想要一个这样的 URL用于Facebook 图形 API 搜索来检索数据。
当我尝试使用这个url时,它给出了
{
"error": {
"message": "(#200) Must have a valid access_token to access this endpoint",
"type": "OAuthException",
"code": 200
}
}
我将在 Android 上使用它。有人可以指导我实现这一目标。
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:
Settings
, click on Edit Settings
.Sandbox Mode
and select Disabled
Settings
section, click on Advanced.Authentication
section, check App Type
and select Web
(if it is not already that)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:
Once a month maybe, you will just need to make a request using this url:
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