简体   繁体   English

Facebook API-Web应用程序的访问令牌

[英]Facebook API - access token for web application

I am making a web app that pulls the latest posts from our Facebook page and processes them. 我正在制作一个网络应用程序,可以从我们的Facebook页面中提取最新帖子并进行处理。 This is all working fine with a hard-coded access token generated from this page. 通过页面生成的硬编码访问令牌,一切都可以正常工作。

The problem is that this token expires, so i am looking for a solution to generate a new token every time the page loads or a non-expiring token - (i have read somewhere that non expiring tokens don't exist anymore). 问题是该令牌已过期,因此我正在寻找一种解决方案,以便在每次页面加载或未过期的令牌时生成新令牌-(我读过某个地方不再存在未过期的令牌)。

So of course i did some research, here , here and here . 因此,我当然在这里这里这里做了一些研究。

But non of these examples seem to be working. 但是这些例子似乎都不起作用。

Before any complaints of some code that i have tried so far, this is my working example - with an expiring access token: 到目前为止,我一直在尝试抱怨某些代码,这是我的工作示例-访问令牌已过期:

var Facebook = function () {
    this.token = 'MYTOKEN';
    this.lastPost = parseInt((new Date().getTime()) / 1000);
    this.posts = [];
};

Facebook.prototype.GetPosts = function () {
    var self = this;
    var deffered = $q.defer();
    var url =  'https://graph.facebook.com/fql?q=SELECT created_time, message, attachment FROM stream WHERE created_time < ' + self.lastPost + ' AND source_id = 437526302958567 ORDER BY created_time desc LIMIT 5&?access_token=' + this.token + '';
    $http.get(url)
        .success(function (response) {
            angular.forEach(response.data, function (post) {
                self.posts.push(new Post(post.message, post.attachment.media, post.attachment.media[0].src, post.created_time, 'facebook'));
            });
            self.lastPost = response.data[response.data.length -1].created_time;
            deffered.resolve(self.posts);
            self.posts = [];
        });
    return deffered.promise;
};
return Facebook;

Any help / suggestion will be greatly appreciated. 任何帮助/建议将不胜感激。

First off, it is important to remember that Facebook has just launched the Version 2 of the Graph API. 首先,重要的是要记住,Facebook刚刚启动了Graph API的版本2。 From April 2014 on, if you have issues with your app, you need to tell us when you created it on Facebook Developers (new apps use the Version 2 by default). 从2014年4月开始,如果您的应用程序出现问题,则需要告诉我们您在Facebook Developers上创建它的时间(默认情况下,新应用程序使用第2版)。

In order manage pages, your app needs to have manage_pages permission. 为了管理页面,您的应用程序需要具有manage_pages权限。 Make sure that the user you want to manage fan pages for has authorized you. 确保您要为其管理粉丝页面的用户已授权您。 If your app uses the Version 2, make sure that Facebook (the Facebook staff) has authorized you to ask users that kind of permission, otherwise your app won't work. 如果您的应用程序使用版本2,请确保Facebook(Facebook工作人员)已授权您向用户询问那种许可,否则您的应用程序将无法运行。

Once you get your token, exchange it for a permanent token (or a token with long expiry date). 获得令牌后,将其交换为永久令牌(或有效期长的令牌)。 Make sure you use the token of the fan page, not the token of the user. 确保使用粉丝页面的令牌,而不是用户的令牌。

If instead you want to read the stream of public fan pages , you need an access token with read_stream permissions. 相反,如果您想阅读公共粉丝页面流 ,则需要具有read_stream权限的访问令牌。 This permission needs to be approved by Facebook (see above) and this specific type of permission takes time to approve, if you're using the Version 2 of the Graph API. 如果您使用的是Graph API第2版,则此权限需要获得Facebook的批准(请参阅上文),并且这种特定类型的权限需要花费一些时间才能批准。 If you're using the old API (Version 1), you can still do that without pre-approval on Facebook's side. 如果您使用的是旧版API(第1版),则无需经过Facebook方面的预先批准,仍然可以执行此操作。 The URL to ask for the permission to read the stream is as follows: https://www.facebook.com/dialog/oauth?client_id=$YOUR_APP_ID&redirect_uri=$YOUR_URL&scope=read_stream,manage_pages (i've added manage_pages in this case, you may not need it). 要求获得读取流权限的URL如下: https://www.facebook.com/dialog/oauth?client_id=$YOUR_APP_ID&redirect_uri=$YOUR_URL&scope=read_stream,manage_pages : https://www.facebook.com/dialog/oauth?client_id=$YOUR_APP_ID&redirect_uri=$YOUR_URL&scope=read_stream,manage_pages client_id https://www.facebook.com/dialog/oauth?client_id=$YOUR_APP_ID&redirect_uri=$YOUR_URL&scope=read_stream,manage_pages $YOUR_APP_ID&redirect_uri https://www.facebook.com/dialog/oauth?client_id=$YOUR_APP_ID&redirect_uri=$YOUR_URL&scope=read_stream,manage_pages $YOUR_URL&scope https://www.facebook.com/dialog/oauth?client_id=$YOUR_APP_ID&redirect_uri=$YOUR_URL&scope=read_stream,manage_pages read_stream,manage_pages(在这种情况下,我已添加了manage_pages,您可能不需要它)。 That url will prompt for authorization. 该网址将提示您进行授权。 Once the user has authorized the app, you'll be recirected to the URL you chose, with a code= variable. 一旦用户授权了该应用程序,您将被重定向到您选择的URL,其中包含code=变量。 At that point, call this other url: 届时,请调用其他网址:

 https://graph.facebook.com/oauth/access_token?client_id={$app_id}&redirect_uri=$someurl&client_secret={$app_secret}&code={$code}

You'll get a response that has the access_token= variable in it. 您将获得一个包含access_token=变量的响应。 Grab that access token, exchange it for a long one, with the following URL: 抓住该访问令牌,将其与以下URL交换一长段:

https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={$app_id}&client_secret={$app_secret}&fb_exchange_token={$token_you_have_just_grabbed}

The response will give you a token that lasts for some time. 响应将为您提供持续一段时间的令牌。 Previously, Facebook had decided to have these "long duration tokens" expire after one month. 此前,Facebook已决定让这些“长期令牌”在一个月后过期。 I have found out, though, that they may have changed their mind: if you put a user token in the debugger, you'll see it never expires. 不过,我发现他们可能改变了主意:如果将用户令牌放入调试器,您将看到它永不过期。 This is the authorization flow for users who visit with a browser. 这是使用浏览器访问的用户的授权流程。 There's the app authorization flow too. 也有应用授权流程。 If all you need is a stream from your own Fan page, you want to do the following (with Graph API V.1): 如果仅需要您自己的粉丝页面中的视频流,则要执行以下操作(使用Graph API V.1):

  • make an HTTP GET request using the following URL: https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret} 使用以下URL发出HTTP GET请求: https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret} : https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}

  • Use the resulting token to make another HTTP GET call, like so: https://graph.facebook.com/{$your_page_id}/feed?{$authToken}&limit=10 //ten posts 使用产生的令牌进行另一个HTTP GET调用,如下所示: https://graph.facebook.com/{$your_page_id}/feed?{$authToken}&limit=10 //ten posts : https://graph.facebook.com/{$your_page_id}/feed?{$authToken}&limit=10 //ten posts $your_page_id}/feed?{$authToken} https://graph.facebook.com/{$your_page_id}/feed?{$authToken}&limit=10 //ten posts

  • Decode the json object 解码json对象

You're done. 你完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM