且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Facebook - 如何获得永久用户访问令牌

更新时间:2023-12-01 20:31:52

我还想在不创建应用程序的情况下访问数据.我尝试了很多东西,但最终我不得不为它创建应用程序.所以你必须创建一个应用程序.

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.

此外,如果您看到开发者网站,它会说他们在 2012 年 12 月删除了offline_access"权限.请参阅此处:https://developers.facebook.com/roadmap/offline-access-removal/.因此,就在那时,他们带来了长期存在的访问令牌.他们还说并且我引用了,如果你想刷新一个仍然有效的长期访问令牌,你必须首先获得一个新的短期用户 access_token,然后在下面调用相同的端点."

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."

因此,您必须再次获取短期访问令牌并使用短期令牌获取长期令牌.要获得长期访问令牌,您可以发送这样的 HTTP 请求:

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();

希望能帮到你