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

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

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

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


当前回答

除了在Vlasec回答中推荐的步骤,您还可以使用:

图形API资源管理器进行查询,例如/{pageId}?= access_token&access_token = THE_ACCESS_TOKEN_PROVIDED_BY_GRAPH_EXPLORER字段 访问令牌调试器以获取有关访问令牌的信息。

其他回答

我找到了这个答案,它指的是这个工具,它真的很有用。

我希望当你读到这篇文章时,这个答案仍然有效。

上面的大多数答案现在都没有给出永久代币,他们只延长到2个月。我是这样得到它的:

从图形资源管理器工具中,选择相关权限并获得短时间页面访问令牌。 转到调试器工具并粘贴您的访问令牌。然后,单击页面底部的“扩展令牌”按钮。 复制扩展的令牌并在这个API中使用它: https://graph.facebook.com/v2.10/me?fields=access_token&access_token= < extended_access_token > 这将返回永久访问令牌。你可以在调试器工具中验证,expires at字段应该说“Never”。

多亏了@donut,我设法在JavaScript中获得了永不过期的访问令牌。

// Initialize exchange
fetch('https://graph.facebook.com/v3.2/oauth/access_token?grant_type=fb_exchange_token&client_id={client_id}&client_secret={client_secret}&fb_exchange_token={short_lived_token}')
.then((data) => {
    return data.json();
})
.then((json) => {
    // Get the user data
    fetch(`https://graph.facebook.com/v3.2/me?access_token=${json.access_token}`)
    .then((data) => {
        return data.json();
    })
    .then((userData) => {
        // Get the page token
        fetch(`https://graph.facebook.com/v3.2/${userData.id}/accounts?access_token=${json.access_token}`)
        .then((data) => {
            return data.json();
        })
        .then((pageToken) => {
            // Save the access token somewhere
            // You'll need it at later point
        })
        .catch((err) => console.error(err))
    })
    .catch((err) => console.error(err))
})
.catch((err) => {
    console.error(err);
})

然后我像这样使用保存的访问令牌

fetch('https://graph.facebook.com/v3.2/{page_id}?fields=fan_count&access_token={token_from_the_data_array}')
.then((data) => {
    return data.json();
})
.then((json) => {
    // Do stuff
})
.catch((err) => console.error(err))

我希望有人能修改这段代码,因为它有点乱,但这是我能想到的唯一方法。

我尝试了这些步骤: https://developers.facebook.com/docs/marketing-api/access#graph-api-explorer

获取永久页面访问令牌

进入图形API资源管理器 在应用程序中选择你的应用程序 将长寿命访问令牌粘贴到access token中 在“访问令牌”旁边,选择要为其设置访问令牌的页面。访问令牌显示为一个新字符串。 单击i查看此访问令牌的属性 再次点击“打开访问令牌工具”按钮,打开“访问令牌调试器”工具,检查属性

一个提示,它只适用于我的网页语言是英语。

应用程序请求限制达到(#4)- FB API v2.1及更高

这个答案把我引向了“我们的终极答案”,所以它非常相关,所以我把它附加在这里。虽然这与上述内容有关,但它是不同的,facebook似乎简化了这一过程。

当FB将api滚到v 2.1时,我们网站上的共享计数停止工作。在我们的例子中,我们已经有一个FB应用程序,我们没有使用FB登录。所以我们需要做的是获得一个FB APP令牌来发出新的请求。截至2016年8月23日。

Go to: https://developers.facebook.com/tools/explorer Select the api version and then use GET and paste the following: /oauth/access_token?client_id={app-id}&client_secret={app-secret}&grant_type=client_credentials You will want to go grab your app id and your app secret from your app page. Main FB Apps developer page Run the graph query and you will see: { "access_token": "app-id|app-token", "token_type": "bearer" } Where "app-id" and "app-token" will be your app id from your FB app page and the generated FB App HASH you just received. Next go test your new APP access token: FB Access Token tester You should see, by pasting the "app-token" into the token tester, a single app based token without an expiration date/time.

在我们的例子中,我们使用FB js sdk,所以我们改变了我们的调用,就像这样(请注意,这只得到分享计数,而不是分享和评论计数的结合,就像它以前一样):

FB.api(
    '/','GET',{
    // this is our FB app token for our FB app 
        access_token: FBAppToken,
        "id":"{$shareUrl}","fields":"id,og_object{ engagement }"
}

这是现在正常工作。这花费了大量的搜索和FB的官方错误报告,以确认我们必须开始向FB api发出令牌化请求。顺便说一句,我确实要求他们(FB)在错误代码(#4)中添加一条提到标记化请求的线索。

我刚从我们的一个开发人员那里得到另一份报告,说我们的FB评论计数也破了,因为需要新的标记化请求,所以我会相应地更新。