Facebook Messenger subscribed apps, page and app access token generation

微笑、不失礼 提交于 2019-12-11 05:08:29

问题


I am hitting a wall while developing seamless integration of a Facebook page with my bot.

Essentially I want to achieve same integration than Chatfuel or Manychat have, where being logged in with your Facebook account lets you to just choose what page you are connecting to them and you are good to go.

The problem I am facing is generating the proper token in order to bind the selected page to my app (bot). As per Facebook documentation:

When you create a subscribed_apps edge, the page-id you use in endpoint must match the page ID of the page access token used in the API call. The app that the access token is for is installed for the page.

Given the call has no other parameter than the access token, this access token has to be enough for Facebook to:

  1. Authorize the action on the page.
  2. Identify what app is being subscribed to the page.

This is confirmed while using the Facebook Graph API Explorer, where one selects the page and the app to bind and a proper access token is generated:

This token properly works using cURL in the terminal:

$ curl -X POST 'https://graph.facebook.com/v3.0/<MY_APP_ID_HERE>/subscribed_apps?access_token=<TOKEN_PASTED_FROM_GRAPH_API_EXPLORER>'
{"success":true}

With the Facebook Access token debugger (info icon on the left of the access token, then open in Access token tool), it is confirmed that the token knows about both the page and the app that have to be connected.

The question is, how are these page-app related token programmatically produced? I can't seem to find the proper API call in Facebook documentation and it is by all means possible, as Chatfuel and Manychat are doing this.

Thanks in advance for your support Lars Schwarz and community!


回答1:


Adding some detail to Alex's answer, for it to be more complete.

When subscribing an app to a page, Facebook needs to:

  1. Know what app you are talking about.
  2. Know what page you are talking about.
  3. Know that you have permissions on that page to subscribe an app.

How does Facebook know it all?

1 comes from the fact that Facebook login happens in the context of a page, actually, the Javascript code for Facebook contains your appId:

js.src = 'https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.0&appId=<YOUR_APP_ID_IS_HERE>&autoLogAppEvents=1';

2 Comes from the page_id in the URL used to subscribe apps to pages:

https://graph.facebook.com/v3.0/YOUR_APP_ID_HERE/subscribed_apps?access_token=YOUR_ACCESS_TOKEN_HERE

3 Comes from the access token, obtained in the context of an APP through Facebook login, that is passed as parameter in the URL used to subscribe apps to pages:

https://graph.facebook.com/v3.0/YOUR_APP_ID_HERE/subscribed_apps?access_token=YOUR_ACCESS_TOKEN_HERE




回答2:


To do this, you need to put FB Login on your site/customer portal and request pages_messaging and manage_pages permissions. The person that logs in must be a Page Admin.

Once your app has been granted that permission for the page, you can generate a page access token as described here:

https://developers.facebook.com/docs/pages/access-tokens



来源:https://stackoverflow.com/questions/51216018/facebook-messenger-subscribed-apps-page-and-app-access-token-generation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!