您的位置:首页 > 其它

如何获取永久性的facebook page access token

2017-03-08 15:03 441 查看
有用的工具:

查询access token的过期时间

https://developers.facebook.com/tools/debug/accesstoken

facebook graph api explorer

https://developers.facebook.com/tools/explorer?method=GET&version=v2.8

流程说明:

如何获取永久的facebook page access token

1 要有个app(app id,app secret)

2 通过graph api explorer获取一个临时的access token,有效期是1个小时

3 通过api

https://graph.facebook.com/oauth/access_token?client_id=APP_ID_HERE&client_secret=APP_SECRET_HERE&grant_type=fb_exchange_token&fb_exchange_token=ACCESS_TOKEN_HERE

获取一个有效期60天的app access token

access_token=xxx&expires=5184000

5184000 就是60天的秒数

4 用60天的app access token 到

https://developers.facebook.com/tools/debug/accesstoken

在访问口令中输入token再点击调试按纽,查看该token的有效期,是60天的有效期。

5 再入到

https://developers.facebook.com/tools/explorer

通过me?id,name 获取到id和name(注意,这个userid必须是app的userid。)

{
"name": "xx",
"id": "xx"
}


将2个月的token 粘贴进token输入框,再更改url为

facebook-userid/accounts


点击submit按钮提交,会获取到这个帐号下所有的page信息。而token对应的page会生成一个新的token,这个token就是不会过期的了。

应用程序编号xxx : xxxxx
用户编号xx:xx
用户上次是通过 APIv2.x 安装此应用的
已签发xxx (7 分钟前)
到期日禁止
有效
来源Unknown
如果在左下角有个按纽,会生成2个月有效期的token。

以下是原文:

Scopes user_birthday, user_religion_politics, user_relationships, user_relationship_details, user_hometown, user_location, user_likes, user_education_history, user_work_history, user_website, user_managed_groups, user_events, user_photos, user_videos, user_friends, user_about_me, user_status, user_games_activity, user_tagged_places, user_posts, read_page_mailboxes, rsvp_event, email, ads_management, ads_read, read_insights, manage_pages, publish_pages, pages_show_list, pages_manage_cta, pages_manage_instant_articles, pages_messaging, pages_messaging_phone_number, publish_actions, read_audience_network_insights, read_custom_friendlists, user_actions.books, user_actions.music, user_actions.video, user_actions.news, user_actions.fitness, public_profile

We’ve been working on upgrading our stats report email that sends our every week detailing key statistics of how that client’s marketing is going. My plan is to have a very robust email detailing key metrics for all the major areas we focus on, including SEO, social media, blogging, email marketing, online ads and more.

To do that I’ve been patching together lots of APIs and database connects to consolidate all the information into this one email. One of those stats I want to display is the client’s Facebook Page views using the insights’ metric page_impressions from the Facebook Graph API.

To do that and allow my weekly email app to run based on a cron job (so it can automatically send out), I needed to get a never expiring access token for this. This took me quite a while to figure out due to the lack of detailed explanation on Facebook’s docs and other places I’d reviewed online. So here is how to get a never expiring Facebook Page Access Token.

Very first thing you need to do is make sure you are an admin/manager of the Facebook page you want a get a never expiring token for. Without that you’re not getting anywhere.

How to get a never expiring Facebook Page Access Token:

Created a Facebook APP or used one I already created.

Wrote down that Facebook APP’s Id and Application Secret.

Got a Short-Lived Access Token from the Facebook Graph API Explorer. Facebook Graph API Explorer can be found here https://developers.facebook.com/tools/explorer

First selected my app as the Application dropdown.

Then clicked Get Access Token button.

When popup opened, I clicked on Extended Permissions and checked boxes for manage_pages, read_insights and read_stream.

Then click Get Access Token on popup window.

This gave me an Access Token in the Access Token box. I copied that down. And tested it with the Facebook Access Token Debugger https://developers.facebook.com/tools/debug/accesstoken just to see if it was a token that would expire and it should expire in less than 1 hour. I didn’t need to test this but wanted to just to see what it would say.

Then I took that Access Token, the APP’s Id and Application Secret and plugged those into this URL https://graph.facebook.com/oauth/access_token?client_id=APP_ID_HERE&client_secret=APP_SECRET_HERE&grant_type=fb_exchange_token&fb_exchange_token=ACCESS_TOKEN_HERE

That URL gave me a Long-Lived Access Token. This expires in 60 days. I tested it to see that using the Facebook Access Token Debugger. Now I tested this Access Token with the Facebook Access Token Debugger and got back that it expires in 2 months (60 days).

Now to get the never expiring Page Access Token, I went to Facebook Graph API Explorer and plugged in that Long-Lived Access Token we just created which expires in 2 months. I pasted this into the Access Token box. Then entered into the Get input box “MY_FACEBOOK_USER_ID/accounts” (without quotes). If you don’t know what your Facebook ID is you can get it here http://findmyfacebookid.com/.

Then I clicked the Submit button on the Facebook Graph API Explorer page. Although when I did that I got back an error. So I put back into the Get input box what was their originally to see if I could get my id. I put this back in “me?fields=id,name” (no quotes).

I got back an id number and my name in the results.

I took that id and plug it into the GET input box “MY_FACEBOOK_USER_ID/accounts” (without quotes). Now I got back a list of pages I manage. Along with an Access Token for each page. This Access Token should be a never expiring token.

To confirm these were never expiring tokens I tested them with the Facebook Access Token Debugger and got back they never expire.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  facebook
相关文章推荐