Facebook non-expiring access token - facebook

I want to show the last facebook post on my website. I have a script for it but it needs access token to work.
I followed the steps mentioned in this post:
https://neosmart-stream.de/de/facebook/how-to-create-a-facebook-access-token/comment-page-1/#comment-617
Now I've got an access token with an expiry period of 60 days but
when I open https://graph.facebook.com/me/accounts, I get this error:
{
"error": {
"message": "An active access token must be used to query information about the current user.",
"type": "OAuthException",
"code": 2500
}
}
as my access token validity is 2 months, It is supposed to work.

1: Create an application in facebook with your website url
2: Run this link in your browser
3: Visit the following to generate a new SHORT-LIVED (1 hour) access token:
https://www.facebook.com?client_id=[APPID]&client_secret=[APPSECRET]&redirect_uri[http://APPURL]&scope=manage_pages,read_stream&response_type=token
4: Take that short-lived access token and send it here:
https://graph.facebook.com/oauth/access_token?client_id=[APP_ID]&client_secret=[APP_SECRET]&grant_type=fb_exchange_token&fb_exchange_token=[EXISTING_ACCESS_TOKEN]
5: Then, either in FB’s Graph API Explorer or on your own, visit here:
https://graph.facebook.com/me/accounts?access_token=[TOKEN]
Then paste the token and id in the code, then you will get the result.
You can debug and verify the expiration date of your token using the FaceBook Access Token Debugger:

when i'll open https://graph.facebook.com/me/accounts...
You need to open it in the Graph API Explorer and add the access token retrieved in the earlier step or append it to end of the url
https://graph.facebook.com/me/accounts?access_token=XXXX

Related

Facebook access token with "never-expire"

I'm trying to get a Facebook access token that never expires but I cannot manage to do it. Here is my steps:
1) Use Facebook graph explorer to generate a short live access token with the desired permissions (this token is valid for 1h or so)
2) Get a long live token (2 months expiry time) by manual GET request to
https://graph.facebook.com/oauth/access_token?
grant_type=fb_exchange_token&
client_id=[app-client-id]&client_secret=[app-secret-id]& fb_exchange_token=[short-live-access-token]
3) An this tohttps://graph.facebook.com/[user-id]?fields=access_token&access_token=[long-live-access-token]
but that last steps show an error:
{
"error": {
"message": "(#100) Tried accessing nonexisting field (access_token) on node type (User)",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "DpmM6CyzoAt"
}
}
I have found that last step somewhere on the net so I presume FB must have change something, I have read their api doc but I cannot figure out by what resource they chance the step 3.
Could someone explain me how to get a user access token that never expires ?
thank you
User Tokens are never valid forever, an extended one is only valid for 60 days as you have found out already. I assume you want to get an Extended Page Token, that´s how you can get one:
https://graph.facebook.com/[page-id]?fields=access_token&access_token=[long-live-user-token]
Basically, you just need to use a Page ID, it does not work with a User ID.

get conversations of FB page via open graph api

I've registered an Facebook-App and created a token with all permissions I need. Among them
manage_pages
read_mailbox
read_page_mailbox
Now I want to use the Facebook open graph API to read some data from my own FB-page. I want to read the private messages that the page received and that I sent to some of the fans on my pages behalf.
I know I can get all information by sending a http-request like this:
https://graph.facebook.com/{page-id}/{object}?access_token={token}
where {something} are placeholders for actual values. To give an working example, I can read the postings that appeared on my page by sending this request:
https://graph.facebook.com/141928949155955/posts?access_token={my secret token}
As I said, this works fine, since more than two years. (I just need to update the token from time to time)
But now I want to read the private conversations between the page and it's fans. I want to extract all conversations and insert them into a spreadsheet. I want to do it once, and maybe in 1 or 2 years again. I could extract them manually from the browser window by copy and paste for each conversation, but since there are so many conversations I think it costs less time to let a program do it for me.
If I understand Facebooks Documentation correct, then the keyword that I must use must be conversations. But I get this:
https://graph.facebook.com/141928949155955/conversations?access_token={secret token}
{
"error": {
"message": "(#210) Subject must be a page.",
"type": "OAuthException",
"code": 210
}
}
But 141928949155955 is a page. I don't know what I'm doing wrong. What is the correct request that I must send to receive a pages private conversations?
EDIT (June 16th):
I can read my personal conversations with this request:
https://graph.facebook.com/me/conversations?access_token={secret token}
But I don't want to read the conversations that I made as a person. I need those of one of my pages.
I found the solution for my problem. (Thanks kush, your answer helped a lot, but I want to add some more information)
My problem was that I misunderstood the error message "Subject must be a page." I thought that "Subject" refers to a part in the query string. But it refers to the token! A message like "Token must be a Page Access Token" would have saved me WEEKS of needless searching over a period of more than two years! How can you use a term like "Subject" when you nowhere define what "Subject" is?
So it was not the query string that was wrong. It was the type of the token!
Now here is the complete procedure to solve the problem (get a Page Access Token)
SOLUTION
Step 1
Get a User Access Token for your App that you can use later to get a Page Access Token
1.1 Go to Graph API Explorer https://developers.facebook.com/tools/explorer/
1.2 Select your App from the "Application:" drop down menu.
1.3 Click "Get Access Token".
1.4 From the tab "Extended Permissions" select "manage_pages".
Additionally select all permissions you need for the Page Access Token. (Although now you don't create a Page Access Token, just a User Access Token)
1.5 Click "Get Access Token".
1.6 Grant the permissions by clicking "OK".
In the field "Access Token:" you find now a Token that is an User Access Token. It is not a Page Access Token! This Token has all permissions you need to manage your page (including the permission "manage_pages"), but you can't use it to manage pages since it is the wrong type of token. You need this token only for one reason: To create a new token.
Step 2
Use the User Access Token you just got to create a Page Access Token
2.1 Enter "me/accounts" in the query field
2.2 Click "Submit"
You get a list of your pages, each with an "access_token" (which is now a Page Access Token). Each of this tokens did inherit the permissions from the User Access Token you used when you called "me/accounts".
This Page Access Token expires after one hour.
supplement
If you want a Page Access Token that never expires, do it this way:
A.1 Go to https://developers.facebook.com
A.2 From the drop down menu "Apps" select the App you want to use.
A.3 Click on "show" in the field "App Secret" (you need to enter your Facebook Password)
A.4 Open a new tab in your browser and there execute steps 1.1 to 1.6 from above to generate a short-lived User Access Token
A.5 Open a third Tab in your browser and there enter this string into the address field of your browser:
https://graph.facebook.com/v2.0/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={short-lived-token} (do not press enter!) If this strings disappears when switching from one browser tab to another use any text editor to assemble the string and copy it later into the address field.
A.6 In this string replace {app-id} and {app-secret} by the values from the browser tab that still shows your Apps data.
A.7 Replace {short-lived-token} with the User Access Token you find in the field "Access Token:" from the Graph API Explorer.
(After this step there are no more curly brackets in the address string)
A.8 send this request (press Enter now)
Your browser window now shows a String with the fields "access_token" and "expires", separated by an ampersand ("&") which is not part of the token. This Token is a long lived User Access Token that will expire in two month.
A.9 Execute Steps 2.1 and 2.2 using the "access_token" from step A.8
The result is a Page Access Tokes with all permissions you selected in Step 1.4 and that never will expire.
Good luck!
Using page access token should help since you are reading from the page ..
Graph api node: GET me/accounts
me/accounts would give you the pages you are admin for and use access token coming in the page info dictionary.
you should get the following data from me/accounts
{
"data": [
{
"category": "Product/service",
"name": "Sample Page",
"access_token": "{page-access-token}",
"id": "1234567890",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
},
}
Use page-access-token to access data from page as the page admin and thou shall receive data :) let me know if this works :)
A successful authorization would return an access token to your app, which you can then use to perform actions to the Facebook API. The error message displayed means you do not have a valid access token which means you probably did not authenticate the app correctly. I would put some logging on the onFacebookError and onError methods to see what the problem is.
Request Permission At Login time So Its Authentication Error
Permission List
https://developers.facebook.com/docs/facebook-login/access-tokens/
Set Permission Reference
https://developers.facebook.com/docs/android/login-with-facebook#permissions

Facebook Open Graph API Examples - How to get the access token?

Facebook has some great-looking examples of Open Graph requests here:
https://developers.facebook.com/docs/reference/api/examples/
The problem is that since I don't have an access token, most of the examples just return
{
"error": {
"message": "An active access token must be used to query information about the current user.",
"type": "OAuthException",
"code": 2500
}
}
I understand access tokens, but how do I get the token so that I can experiment with these example requests?
As long as you only want to test...
Use the Graph API Explorer to generate a token with the permissions you want.
Don't hesitate to select all of them.
Then, just copy/paste the token at the end of each graph.facebook.com URL. For instance:
https://graph.facebook.com/v1.0/me/friends?access_token=<ACCESS_TOKEN>
The day you want to get the access taken for an app...
Start by reading this doc.

Facebook access token extending - "The access token does not belong to application xxx"

I went to https://developers.facebook.com/tools/explorer?method=GET, set up there ID of my Facebook page, set up there the correct permission for posting statuses on my FB timeline and generated access token.
Then I wanted to extend this short-live token on the long-term (60 days long valid token). So I did following - I put this URL to the browser:
https://graph.facebook.com/oauth/access_token?client_id=APP_ID_OF_MY_FB_APP&client_secret=SECRET_ID_OF_MY_APP&grant_type=fb_exchange_token&fb_exchange_token=GENERATED_ACCESS_TOKEN_FROM_THE_FIRST_STEP
and in the browser I saw this error message:
{
"error": {
"message": "The access token does not belong to application APP_ID_OF_MY_FB_APP",
"type": "OAuthException",
"code": 1
}
}
Which is weird, because the APP_ID of my Facebook app is correct, I've tried to post on the wall of the Facebook page and it was working.
But when I try to get extended access token, I am getting the error above.
What's wrong with the access?
Thank you
This almost certainly means what the error says, which is that GENERATED_ACCESS_TOKEN_FROM_THE_FIRST_STEP is not actually from the app ID that you're using in the APP_ID_OF_MY_FB_APP parameter
Check the access token you're trying to extend in Facebook's Debug Tooland ensure it was actually generated for your app
Your AppID param is not the same to the AppID in developers.facebook.com

Facebook Graph API Error (access token)

I'm getting the following error when I try to access the Graph API with my access token:
{
"error": {
"message": "Expected 1 '.' in the input between the postcard and the payload",
"type": "OAuthException",
"code": 1
}
}
I'm grabbing the access token after successfully authenticating an app I'm working on. I have accepted the permissions and I'm hitting the app's landing page. The access token doesn't look like others I've seen online. It doesn't have the '|' character in it. It does have a period and some underscores along with the typical alphanumeric combination.
I figured out the problem. The other developer I was working with used part of the raw signed_request object (not parsed) to form the access token. After taken a gander at the documentation, I quickly realized that you need to parse the signed_request object first before obtaining the oauth_token value. After that was fixed, I was able to easily make an API call.
Check to ensure you are verifying the "code" parameter returned by Facebook before signing the request, not the "access token". Signing a request with a bad access token produced this error message for me.