Facebook - how to get permanent user access token

2019-05-05 06:16发布

This question already has an answer here:

My company has a corporate facebook account where they post event pics in different albums. My requirement is to get all the photos from facebook albums and show in our corporate website. I am able to fetch the photos but the user access token is valid only for 60 days which means every other two months i will have to login into company's corporate facebook account, regenerate the token and update the token in my application.

Is there any way to generate to permanent access token ? or is ther any way to regenerate the token at my application level ( without showing login dialog box) ?

is there any other way to fetch the photos without creating app ?

I followed following steps: https://www.facebook.com/dialog/oauth?client_id=%3CCLIENT_ID%3E&scope=manage_pages&redirect_uri= I got code from this API call. Then I got token as follows : https://graph.facebook.com/oauth/access_token?client_id=&redirect_uri=&client_secret=&code= I verified the token using Access token Debugger. Token is valid for 60 days. To get page token, i used url: graph.facebook.com/me/accounts?access_token= I got token for my app page. I verified this token in tool, Its valid only for 60 days

thanks

1条回答
神经病院院长
2楼-- · 2019-05-05 06:40

I also wanted to access the data without creating an app. I tried a lot of things, but finally I had to create for app just for the sake of it. So you would have to create an app.

Also, If you see the developers website, it says that they removed the "offline_access " permission in December 2012. See here: https://developers.facebook.com/roadmap/offline-access-removal/ . So, it was then they brought the long-lived access token. They also say and I quote, " If you would like to refresh a still valid long-lived access_token, you will have to get a new short-lived user access_token first and then call the same endpoint below."

So you would have to get the short term access token again and get the long-lived token using the short lived token. To get the long term access token, You may send a HTTP request like this:

var short_access_token; //Get this
var xhr = new XMLHttpRequest();
var f_url = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=CLIENT_ID&client_secret=APP_CLIENT_SECRET&fb_exchange_token="+short_access_token;

xhr.open("GET", f_url , true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        obj = xhr.responseText;
        long_token =  obj.split('=')[1].split('&')[0];
    }
}
xhr.send();

Hope it helps

查看更多
登录 后发表回答