使用 Facebook Javascript SDK 获取图形数据

发布于 2024-11-19 01:14:04 字数 1643 浏览 3 评论 0原文

现在修复了!但我还不能回答我自己的问题。请参阅下面我的评论。感谢您的帮助。


我搜索了又搜索并阅读了文档,但仍然无法弄清楚这一点。

我有一个关于活动的网页。我的活动还有一个公共 Facebook“活动”。我正在尝试使用 FB Javascript SDK 获取 Facebook 活动的参加者人数,并将其添加到通过网站注册的人数中。

我创建了一个应用程序,并且有一个 appID 和秘密字符串。我可以从以下位置获取访问令牌:

https://graph.facebook.com/ oauth/access_token?client_id=XXXX&client_secret=XXXXX&grant_type=client_credentials

然后我可以使用该访问令牌获取公共活动的参加者:

https://graph.facebook.com/331218348435/attending?access_token=XXXXXXXXX< /a>

没关系。

我现在正在尝试使用 Javascript SDK 做同样的事情。

我已经加载了 SDK 并完成了初始化:

FB.init({
  appId  : 'XXXXXXXX',
  status : true, // check login status
  cookie : true, // enable cookies to allow the server to access the session
  xfbml  : true  // parse XFBML
});

并且我知道 SDK 正在工作,因为我可以获取包含不需要访问令牌的数据的对象:

FB.api( '/331218348435', function (response) { console.log ( response ) } );

但是当我尝试获取需要访问令牌的与会者数据时:

FB.api( '/331218348435/attending', function (response) { console.log ( response ) } );

我收到 OAuthException:“请求此资源需要访问令牌。”

我能找到的所有教程和信息都涉及使用 .login 方法,但我不希望用户登录,我想使用我的应用程序 ID 登录,而不需要任何用户交互。

我假设 API 接受了 SDK 的 init 请求,并在我调用 .init 方法时授予了我一个访问令牌,身份验证是针对我的网站地址(HTTP 引荐来源网址 - 是的,我已经在 Facebook 应用程序中设置了我的网站 URL)设置)。

有什么想法可能导致此功能不起作用吗?如何使用 Javascript SDK 获取访问令牌而不进行 .login?我是不是少了一步?这可能吗?

谢谢

FIXED NOW! But I can't answer my own question yet. See my comment below. And thanks for helping.


I've searched and searched and read the docs and still can't figure this out.

I have a web page about an event. There's also a public Facebook "event" for my event. I'm trying to use the FB Javascript SDK to get the number of attendees for the Facebook event and add it to the number of people who've registered through the website.

I've created an app and I have an appID and secret string. I can get an access token from:

https://graph.facebook.com/oauth/access_token?client_id=XXXX&client_secret=XXXXX&grant_type=client_credentials

and I can then use that access token to get the attendees for a public event:

https://graph.facebook.com/331218348435/attending?access_token=XXXXXXXXX

That's all fine.

I'm now trying to do this same thing using the Javascript SDK.

I've loaded the SDK and done an init:

FB.init({
  appId  : 'XXXXXXXX',
  status : true, // check login status
  cookie : true, // enable cookies to allow the server to access the session
  xfbml  : true  // parse XFBML
});

and I know the SDK is working because I can get an object with the data that doesn't need an access token:

FB.api( '/331218348435', function (response) { console.log ( response ) } );

but when I try to get the attendee data that needs the access token:

FB.api( '/331218348435/attending', function (response) { console.log ( response ) } );

I get an OAuthException: "An access token is required to request this resource."

All the tutorials and information I can find all refers to using the .login method, but I don't want a user to login, I want to login using my app ID without any user interaction.

I'd assumed that the API took the SDK's init request and granted me an access token when I called the .init method, the authentication being done against my website's address (the HTTP referrer - yes I have set my website URL in the Facebook app settings).

Any ideas what might be causing this to not work? How can I get the access token using the Javascript SDK without doing a .login? Am I missing a step? Is this even possible?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-11-26 01:14:04

根据相当循环的文档所述,获取参与提要需要“通用 access_token”。用 Facebook 的话来说:

任何有效的 access_token
我们的 API 返回的任何有效访问令牌。例如,如果访问令牌已过期,则该访问令牌可能无效。不需要特殊权限。有时,这被称为通用 access_token。

因此,这意味着只要活动是公开的,您就可以使用任何您喜欢的令牌来访问参加的提要。最容易获得的访问令牌似乎是应用程序令牌: http://developers.facebook.com /docs/authentication/#applogin。您只需使用 App ID 和 Secret 即可获取此令牌,无需用户交互。

要总结链接内容:您可以通过发送 GET 请求来获取应用程序访问令牌

https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials 

,然后您可以使用该 access_token 为您参加的列表进行呼叫

FB.api('MyEvent/attending?access_token=ACCESS_TOKEN');

Form what the rather circular documentations says, getting the attending feed requires a 'generic access_token`. In Facebook terms:

Any valid access_token
Any valid access token returned by our APIs. An access token may not be valid if, for example, it has expired. No special permissions are required. Occasionally, this is referred to as a generic access_token.

So this means that you can use any token you like to access the attending feed, as long as the event is public. The easiest access token to get seems to be an app token: http://developers.facebook.com/docs/authentication/#applogin. You can get this token using only your App ID and Secret, and no user interaction is required.

To summerise the link content: You can get an application access token by sending a GET request to

https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials 

You can then use that access_token to make the call for you attending list

FB.api('MyEvent/attending?access_token=ACCESS_TOKEN');
烟沫凡尘 2024-11-26 01:14:04

哦。好的。

好吧,无论出于什么原因,我离开并吃过晚饭,当我回来时一切正常。

当我更新应用程序的设置时,Facebook 表示更改可能需要几分钟才能绕过服务器。结果花了一个多小时!

我的代码很好。

感谢您的帮助。

Oh. OK.

Well, for whatever reason, I went away and had my dinner and when I come back it's working fine.

When I updated the settings for my app Facebook said it might take a few minutes for the change to get around the servers. Turned out to take over an hour!

My code was fine.

Thanks for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文