Facebook Open Graph API 调用出现问题...需要主动访问令牌吗?

发布于 2024-11-02 21:04:34 字数 905 浏览 0 评论 0原文

我在我的网站上使用 Facebook 的开放图 api 以及 JavaScript SDK。我的目标是让用户连接到我的网站,然后在我的网站上执行某些操作后向用户 Facebook 流发布一条消息。我的 Facebook 连接功能运行良好。在连接过程中,我请求电子邮件、publish_stream、offline_access 权限。我想这些就是我想做的事情所需要的全部。现在,当用户通过 facebook connect 连接时,我确实获取并捕获了 facebook 访问令牌和秘密,尽管我不知道如何处理它们。

根据 Facebook 的 JavaScript SDK 伪劣文档...要做我想做的事情,它说使用以下代码:

var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
  if (!response || response.error) {
    alert('Error occured');
  } else {
    alert('Post ID: ' + response.id);
  }
});

我只是将其作为测试,而不是警告响应,而是对 JSON 进行字符串化响应并写入控制台。可以说流发布失败并且响应是这样的:

{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}

如果用户通过 facebook connect “连接”到我的网站并接受我指定的所有权限,那么我没有有效的访问令牌吗?访问令牌是我必须传递给 api 调用的另一个参数吗?

I'm using Facebook's open graph api along with the JavaScript SDK on my site. My goal is for a user to connect to my site and then after performing certain actions on my site publish a message to the users facebook stream. I have the facebook connect function working just fine. During the connect I request email, publish_stream, offline_access permissions. I think those are all I need for what I'm trying to do. Now when the user connects via facebook connect I do get and capture the facebook access token and secret though I don't know what to do with them.

According to Facebook's shoddy documentation for the JavaScript SDK... to do what I'm trying to do it says to use the following code:

var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
  if (!response || response.error) {
    alert('Error occured');
  } else {
    alert('Post ID: ' + response.id);
  }
});

I'm doing just that as a test and instead of alerting the response I'm stringifying the JSON response and writing to the console. Suffice it to say the stream publish fails and the response is this:

{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}

If the user is 'connected' to my site via facebook connect and accepted all the permissions I specified, don't I have a valid access token? Is the access token another parameter I have to pass to the api call?

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

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

发布评论

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

评论(2

顾忌 2024-11-09 21:04:34

确保页面中包含所有这些内容:

<BODY>
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script type="text/javascript">
    FB.init({
      appId  : APP_ID, // the app ID you get when you register your app in http://www.facebook.com/developers/
      status : true, // check login status
      cookie : true, // enable cookies to allow the server to access the session
      xfbml  : true  // parse XFBML
    });

    if (FB.getSession() != null) {
      var body = 'Reading Connect JS documentation';
      FB.api('/me/feed', 'post', { message: body }, function(response) {
        if (!response || response.error) {
          alert('Error occured');
        } else {
          alert('Post ID: ' + response.id);
        }
      });
    }
    </script>

Make sure you have all this in the page:

<BODY>
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script type="text/javascript">
    FB.init({
      appId  : APP_ID, // the app ID you get when you register your app in http://www.facebook.com/developers/
      status : true, // check login status
      cookie : true, // enable cookies to allow the server to access the session
      xfbml  : true  // parse XFBML
    });

    if (FB.getSession() != null) {
      var body = 'Reading Connect JS documentation';
      FB.api('/me/feed', 'post', { message: body }, function(response) {
        if (!response || response.error) {
          alert('Error occured');
        } else {
          alert('Post ID: ' + response.id);
        }
      });
    }
    </script>
沫尐诺 2024-11-09 21:04:34

上面的代码对我来说适用于publish_stream权限。确保在 javascript api 收到访问令牌之前代码未运行。 直接在 FB.api 调用上方运行:

console.log(FB.getSession()) 

以确保您拥有 access_token。

The above code worked for me with the publish_stream permission. Make sure that the code is not running before the javascript api has received the access token. Run:

console.log(FB.getSession()) 

directly above the FB.api call to make sure you have an access_token.

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