简体   繁体   English

如何通过 API 让 Facebook 应用程序访问 Facebook 页面?

[英]How can I give the Facebook App access to the Facebook Page via the API?

As we all know, inside the Facebook for Developers interface, you can add Facebook Pages to a Facebook App as in the picture below and generate the Page Access Token .众所周知,在Facebook for Developers界面中,您可以将 Facebook Pages 添加到 Facebook App 中,如下图所示,并生成Page Access Token

在此处输入图像描述

I'm trying to do this programmatically via the API requests.我正在尝试通过 API 请求以编程方式执行此操作。 Unfortunately, I did not find in the documentation of the request how to do this.不幸的是,我没有在请求的文档中找到如何执行此操作。

What have I done so far?到目前为止我做了什么?

  • I can get the User ID and User Access Token via Facebook Login ( Documentation ).我可以通过Facebook Login文档)获取User IDUser Access Token
  • I can get the list of Facebook Pages that a person owns.我可以得到一个人拥有的Facebook Pages列表。 In the response, I have the Page ID and the Page Access Token ( Documentation ).在响应中,我有Page IDPage Access Token文档)。
  • I have the Facebook app that is in development mode.我有处于开发模式的 Facebook 应用程序。 That app has App ID and App Secret .该应用程序具有App IDApp Secret With these values, I can get the App Access Token ( Documentation ).使用这些值,我可以获得App Access Token文档)。
  • I can set Webhook to the Facebook App with App ID and App Access Token ( Documentation ).我可以使用App IDApp Access Token文档)将Webhook设置为 Facebook 应用。
  • I can set the Webhook Subscriptions Fields for my Facebook App ( Documentation ).我可以为我的 Facebook 应用程序( 文档)设置Webhook Subscriptions Fields

Question: What kind of API request should I use to add a Facebook Page to the Facebook App?问:我应该使用哪种 API 请求将 Facebook 页面添加到 Facebook 应用程序?

在此处输入图像描述

The list of my requests:我的请求清单:

  1. I take Page ID and Page Access Token with this GET request cause this request returns the list of Facebook Pages that a person owns:我使用此 GET 请求获取Page IDPage Access Token ,因为此请求返回一个人拥有的Facebook Pages列表:

https://graph.facebook.com/v9.0/{user-id}/accounts?access_token={user-access-token} https://graph.facebook.com/v9.0/{user-id}/accounts?access_token={user-access-token}

  1. I set the Webhook in my Facebook App with this POST request:我使用以下POST请求在我的Facebook App中设置了Webhook

https://graph.facebook.com/v9.0/{app-id}/subscriptions?access_token={app-access-token}&callback_url={url-address}&verify_token={verify-token}&object=page&include_values=true https://graph.facebook.com/v9.0/{app-id}/subscriptions?access_token={app-access-token}&callback_url={url-address}&verify_token={verify-token}&object=

It successfully works and I see this Webhook in the "Webhooks" block of the Dashboard interface.它成功运行,我在Dashboard界面的“Webhooks”块中看到了这个 Webhook。

  1. Then I make this POST request to set Webhook Subscriptions Fields :然后我发出这个 POST 请求来设置Webhook Subscriptions Fields

https://graph.facebook.com/{page-id}/subscribed_apps?subscribed_fields=messages,messaging_postbacks,messaging_optins,message_deliveries,message_reads,messaging_payments,messaging_pre_checkouts,messaging_checkout_updates,messaging_account_linking,messaging_referrals,message_echoes,messaging_game_plays,standby,messaging_handovers,messaging_policy_enforcement,message_reactions,inbox_labels&access_token={page-access-token} https://graph.facebook.com/{page-id}/subscribed_apps?subscribed_fields=messages,messaging_postbacks,messaging_optins,message_deliveries,message_reads,messaging_payments,messaging_pre_checkouts,messaging_checkout_updates,messaging_account_linking,messaging_referrals,message_echoes,messaging_game_plays,standby,messaging_handovers,messaging_policy_enforcement, message_reactions,inbox_labels&access_token={page-access-token}

In this request, I use Page ID and Page Access Token from the first step.在此请求中,我使用第一步中的Page IDPage Access Token

Unfortunately, I have such an error message:不幸的是,我有这样一个错误信息:

To subscribe to the messages field, one of these permissions is needed: pages_messaging要订阅消息字段,需要以下权限之一: pages_messaging

I've been following down a similar rabbit hole.我一直在跟踪一个类似的兔子洞。 Indeed, Facebook documentation is confusing, but it ended up being pretty simple.确实,Facebook 文档令人困惑,但它最终变得非常简单。 Here is the modified Facebook Login example , which gets page access token and then adds necessary webhook subscriptions for page messaging.这是修改后的Facebook 登录示例,它获取页面访问令牌,然后为页面消息添加必要的 webhook 订阅。 After you've run it, you will see the page is added to the App settings with the requested webhook subscriptions.运行后,您将看到该页面已添加到带有请求的 webhook 订阅的应用程序设置中。 Hope it helps希望能帮助到你

 <.DOCTYPE html> <html> <head> <title>Facebook Login JavaScript Example</title> <meta charset="UTF-8" /> </head> <body> <script> let page_access_token = null let page_id = null function statusChangeCallback(response) { // Called with the results from FB.getLoginStatus(). console.log('statusChangeCallback') console.log(response) // The current login status of the person. if (response.status === 'connected') { // Logged into your webpage and Facebook. testAPI() } else { // Not logged into your webpage or we are unable to tell. document.getElementById('status').innerHTML = 'Please log ' + 'into this webpage.' } } function checkLoginState() { // Called when a person is finished with the Login Button. FB.getLoginStatus(function (response) { // See the onlogin handler statusChangeCallback(response) }) } window.fbAsyncInit = function () { FB:init({ appId, 'YOUR_APP_ID': cookie, true. // Enable cookies to allow the server to access the session: xfbml, true. // Parse social plugins on this webpage: version. 'v12,0'. // Use this Graph API version for this call. }) FB.getLoginStatus(function (response) { // Called after the JS SDK has been initialized. statusChangeCallback(response) // Returns the login status. }) } // add webhooks to page subscriptions function addPageSubscriptions() { FB,api( `/${page_id}/subscribed_apps`, 'POST': { subscribed_fields, [ 'messages': // any other webhook event: https.//developers.facebook,com/docs/messenger-platform/webhook/#events ]: access_token, page_access_token, }. function (response) { if (response &&.response.error) { console.log({ response }) } else { console,error(response.error) } }, ) } // pages I have access to function getPages() { FB.api('/me/accounts'. function (response) { if (response &&.response.error) { console.log({ response }) page_access_token = response.data[0].access_token page_id = response.data[0].id addPageSubscriptions() } else { console.error(response.error) } }) } function testAPI() { // Testing Graph API after login. See statusChangeCallback() for when this call is made. console.log('Welcome. Fetching your information.,.: ') // Me FB.api('/me'. function (response) { console.log('Successful login for, ' + response.name) document:getElementById('status'):innerHTML = 'Thanks for logging in, ' + response,name + ',' getPages() }) } </script> <;-- The JS SDK Login Button --> <:-- IMPORTANT: Define the scopes for managing pages metadata and pages_messaging for the webhooks --> <fb.login-button scope="public_profile.email,pages_manage_metadata,pages_messaging" onlogin="checkLoginState();" > </fb:login-button> <div id="status"></div> <!-- Load the JS SDK asynchronously --> <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js" ></script> </body> </html>

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

相关问题 如何授予我的Facebook应用访问文章的权限? - How do I give my facebook app access to post to my facebook page? 我可以在不创建应用程序的情况下使用 Graph API 访问 Facebook 页面吗? - Can I access a Facebook Page with the Graph API without creating an app? 如何通过Facebook的Connect API将视频发布到我的Facebook个人资料页面? - How can I post a video, via Facebook's Connect API, to my Facebook profile page? 如何在Facebook应用中使用Facebook Graph API在粉丝页面上回复私人消息? - How can I reply to a private message on a fan page using the Facebook Graph API in a Facebook app? Facebook Graph api如何通过访问令牌获取页面ID - Facebook Graph api How to get page ID via an access token 如何将Facebook应用程序与现有Facebook页面关联? - How can I connect a Facebook app with an existing Facebook page? 通过API将Facebook应用添加到页面 - Add a Facebook App to a Page via API 无法通过API访问Facebook广告 - Can't access Facebook ads via api 如何通过API /文本访问Facebook的“赞”? - How do I access Facebook 'likes' via API/text? 如何使用 Facebook API,以及如何通过我的应用在用户墙上发布状态链接? - How to use the Facebook API, and how can I post a link with status on the users wall via my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM