facebook:永久访问令牌? [英] facebook: permanent Page Access Token?

查看:306
本文介绍了facebook:永久访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个将Facebook页面作为其数据源之一的项目进行工作。它定期导入一些数据,而不涉及GUI。然后我们使用网络应用程序来显示我们已经拥有的数据。



并不是所有的信息都是公开的。这意味着我必须一次访问数据,然后保留。但是,我不知道这个过程,我还没有找到一个很好的教程。我想我需要一个 access_token ,我如何从用户一步一步地获得它?用户是Facebook页面的管理员,他是否必须添加一些我们的FB应用程序到页面?



编辑:谢谢@phwd的提示。我做了一个教程如何获得永久页面访问令牌,即使 offline_access 不再存在。



编辑:我刚刚发现在这里回答:持久的FB访问令牌用于服务器拉FB页面信息

解决方案

按照Facebook的扩展页面令牌文档我能够获取页面访问令牌这不会过期。



我建议使用图所有这些步骤的API资源管理器,除非另有说明。



0。创建Facebook应用程序



如果您已经有应用程序,请跳到步骤1。


  1. 转到我的应用

  2. 点击+添加新应用。

  3. 设置网站应用。

您不需要更改其权限或任何内容。您只需要一个在完成访问令牌之前不会消失的应用程序。



1。获取用户短期访问令牌




  1. 转到图形API资源管理器

  2. 选择要获取访问令牌的应用程序(在应用程序下拉菜单中,而不是我的应用菜单)。

  3. 点击获取令牌>获取用户访问令牌。

  4. 在弹出的窗口中, 扩展权限选项卡,选中manage_pages。

  5. 单击获取访问令牌。

  6. 从有权访问的Facebook帐户授予访问权限管理目标页面。请注意,如果此用户失去访问权限,最终的永久失效访问令牌可能会停止工作。

出现在访问令牌字段是您的短命令访问令牌。



2。生成长期访问令牌



以下这些说明从Facebook文档中获取GET请求


https://graph.facebook.com/v2.10/oauth/access_token?grant_type=fb_exchange_token& ; client_id = {app_id} & client_secret = {app_secret} & fb_exchange_token = {short_lived_token}


输入您的应用程序的ID和密码以及上一步中生成的短命令。



无法使用Graph API资源管理器。由于某种原因,它被困在这个请求上。我认为这是因为响应不是JSON,而是一个查询字符串。由于它是一个GET请求,您只需访问浏览器中的URL。



响应应如下所示:


access_token = ABC123 & expires = 5182959


ABC123将是您长久的访问令牌。您可以将其放入访问令牌调试器进行验证。在过期下,应该有2个月的东西。



3。获取用户ID



使用长期访问令牌,向

发送GET请求


https://graph.facebook.com/v2.10/me?access_token = {long_lived_access_token}


id 字段是您的帐户ID。您需要下一步。



4。获取永久页面访问令牌



发送GET请求


https://graph.facebook.com/v2.10/ {account_id} / accounts?access_token = {long_lived_access_token}


JSON响应应该有一个数据字段,其中是用户可以访问的项目数组。找到您要永久访问令牌的页面的项目。 access_token 字段应该有您的永久访问令牌。复制并在访问令牌调试器中进行测试。在到期下,应该说从不。


I work on a project that has facebook pages as one of its data sources. It imports some data from it periodically with no GUI involved. Then we use a web app to show the data we already have.

Not all the information is public. This means I have to get access to the data once and then keep it. However, I don't know the process and I haven't found a good tutorial on that yet. I guess I need an access_token, how can I get it from the user, step by step? The user is an admin of a facebook page, will he have to add some FB app of ours to the page?

EDIT: Thanks @phwd for the tip. I made a tutorial how to get a permanent page access token, even with offline_access no longer existing.

EDIT: I just found out it's answered here: Long-lasting FB access-token for server to pull FB page info

解决方案

Following the instructions laid out in Facebook's extending page tokens documentation I was able to get a page access token that does not expire.

I suggest using the Graph API Explorer for all of these steps except where otherwise stated.

0. Create Facebook App

If you already have an app, skip to step 1.

  1. Go to My Apps.
  2. Click "+ Add a New App".
  3. Setup a website app.

You don't need to change its permissions or anything. You just need an app that wont go away before you're done with your access token.

1. Get User Short-Lived Access Token

  1. Go to the Graph API Explorer.
  2. Select the application you want to get the access token for (in the "Application" drop-down menu, not the "My Apps" menu).
  3. Click "Get Token" > "Get User Access Token".
  4. In the pop-up, under the "Extended Permissions" tab, check "manage_pages".
  5. Click "Get Access Token".
  6. Grant access from a Facebook account that has access to manage the target page. Note that if this user loses access the final, never-expiring access token will likely stop working.

The token that appears in the "Access Token" field is your short-lived access token.

2. Generate Long-Lived Access Token

Following these instructions from the Facebook docs, make a GET request to

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

entering in your app's ID and secret and the short-lived token generated in the previous step.

You cannot use the Graph API Explorer. For some reason it gets stuck on this request. I think it's because the response isn't JSON, but a query string. Since it's a GET request, you can just go to the URL in your browser.

The response should look like this:

access_token=ABC123&expires=5182959

"ABC123" will be your long-lived access token. You can put it into the Access Token Debugger to verify. Under "Expires" it should have something like "2 months".

3. Get User ID

Using the long-lived access token, make a GET request to

https://graph.facebook.com/v2.10/me?access_token={long_lived_access_token}

The id field is your account ID. You'll need it for the next step.

4. Get Permanent Page Access Token

Make a GET request to

https://graph.facebook.com/v2.10/{account_id}/accounts?access_token={long_lived_access_token}

The JSON response should have a data field under which is an array of items the user has access to. Find the item for the page you want the permanent access token from. The access_token field should have your permanent access token. Copy it and test it in the Access Token Debugger. Under "Expires" it should say "Never".

这篇关于facebook:永久访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆