众所周知,Facebook for Developers里面界面,您可以将 Facebook 页面添加到 Facebook 应用程序,如下图所示,并生成 Page Access Token
。
我正在尝试通过 API 请求以编程方式执行此操作。不幸的是,我没有在请求的文档中找到如何执行此操作。
到目前为止我做了什么?
- 我可以通过
Facebook 登录
( Documentation ) 获取User ID
和User Access Token
。 - 我可以获得某人拥有的
Facebook 页面
的列表。在响应中,我有Page ID
和Page Access Token
( Documentation )。 - 我有处于开发模式的 Facebook 应用程序。该应用程序具有
App ID
和App Secret
。使用这些值,我可以获得App Access Token
( Documentation )。 - 我可以使用
App ID
和App Access Token
( Documentation ) 将Webhook
设置到 Facebook App。 - 我可以为我的 Facebook 应用程序 ( Documentation ) 设置
Webhook 订阅字段
。
问题:我应该使用哪种 API 请求将 Facebook 页面添加到 Facebook 应用程序?
我的请求列表:
- 我将
页面 ID
和页面访问 token
与此 GET 请求一起使用,因为此请求返回一个人拥有的Facebook 页面
列表:
https://graph.facebook.com/v9.0/{user-id}/accounts?access_token={user-access-token}
- 我使用此
POST
请求在我的Facebook 应用
中设置了Webhook
:
它成功运行,我在 Dashboard
界面的“Webhooks” block 中看到了这个 Webhook。
- 然后我发出此 POST 请求以设置
Webhook 订阅字段
:
在此请求中,我使用了第一步中的Page ID
和Page Access Token
。
不幸的是,我有这样的错误信息:
To subscribe to the messages field, one of these permissions is needed: pages_messaging
最佳答案
我一直在追踪类似的兔子洞。确实,Facebook 的文档很困惑,但最终变得非常简单。这是修改后的 Facebook Login example ,它获取页面访问 token ,然后为页面消息添加必要的 webhook 订阅。运行它后,您会看到该页面已添加到应用程序设置中,其中包含请求的 webhook 订阅。希望对你有帮助🤓
<!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>
关于facebook - 如何让 Facebook 应用程序通过 API 访问 Facebook 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65924939/