使用 AJAX/jQuery 发布到 Facebook 墙/提要

发布于 2024-10-03 02:21:49 字数 1179 浏览 3 评论 0原文

把我的头发拉到这个上面。

我正在编写一个与 Facebook 连接的 Web 应用程序,该应用程序最终会将一些任意信息发布到整个过程中选择的朋友墙上。

我现在处于最后阶段,希望发布到墙上会很简单,但我现在花了太长时间试图解决这个问题,所以我希望有人可以帮助我。

我试图像这样使用 ajax 发布:

$.ajax({
    type: 'POST',
    url: "https://graph.facebook.com/bbeckford/feed",
    data: {message: wallMessage, target_id: friendID, access_token: "<?= $cookie['access_token'] ?>", format: "json"},
    success: function(data) { alert(data); },
    dataType: "JSON"
});

但我不断收到此错误:“XMLHttpRequest 无法加载 https: //graph.facebook.com/bbeckford/feed。来源http://www.secretsantasetup.com Access-Control-Allow-Origin 不允许。”

我已经进行了搜索,一个建议是制作一个 php 代理,这是一个可行的选择吗?我该怎么做呢?

我的做法完全错误吗?

任何帮助将不胜感激, 谢谢, -本

编辑 我想在后台执行此操作,例如,用户选择了 10 个朋友,然后在提交时应用程序将循环遍历每个朋友并在他们的墙上发布一些内容。这可能吗? 谢谢!

编辑2 下一页底部的测试控制台完全符合我想要的功能,但是没有源代码? - http://developers.facebook.com/docs/reference/rest/stream。发布

pulling my hair out over this one.

I'm writing a Facebook-connected web app that will ultimately post some arbitrary information to the walls of friends that are selected throughout the process.

I'm in the final stages now and was hoping that posting to the walls would be simple, but I've spent far too long trying to figure this out now so I'm hoping someone can help me.

I'm trying to post using ajax like so:

$.ajax({
    type: 'POST',
    url: "https://graph.facebook.com/bbeckford/feed",
    data: {message: wallMessage, target_id: friendID, access_token: "<?= $cookie['access_token'] ?>", format: "json"},
    success: function(data) { alert(data); },
    dataType: "JSON"
});

But I just keep getting this error: "XMLHttpRequest cannot load https://graph.facebook.com/bbeckford/feed. Origin http://www.secretsantasetup.com is not allowed by Access-Control-Allow-Origin."

I've done searches and one suggestion is to make a php proxy, is that a viable option? How would I go about doing that?

Am I approaching this completely wrong??

Any help would be greatly appreciated,
Thanks,
-Ben

EDIT
I want to do this in the background, ie user has selected 10 friends for example, then on submit the app will loop through each friend and post something on their wall. Is this possible?
Thanks!

EDIT 2
The test console at the bottom of the following page does exactly what I want to do, but there is no source code? - http://developers.facebook.com/docs/reference/rest/stream.publish

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

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

发布评论

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

评论(4

清浅ˋ旧时光 2024-10-10 02:21:49

在这里,您可以使用 javaScript SDK

第 1 步

首先,您的应用程序应该获得在用户墙或用户朋友墙上发帖的权限。

来自 Facebook 的示例代码:链接

FB.login(function(response) {
  if (response.session) {
    if (response.perms) {
      // user is logged in and granted some permissions.
      // perms is a comma separated list of granted permissions
    } else {
      // user is logged in, but did not grant any permissions
    }
  } else {
    // user is not logged in
  }
}, {perms:'read_stream,publish_stream,offline_access'});

publish_stream 需要在用户或用户的上发布朋友墙。

需要编辑的行:{perms:'read_stream,publish_stream,offline_access'})

要了解有关其他权限的更多信息:链接

第 2 步

取自 facebook JavaScript SDK 页面并进行调整link

   FB.ui(
   {
     method: 'stream.publish',
     message: 'getting educated about Facebook Connect',
     attachment: {
       name: 'Connect',
       caption: 'The Facebook Connect JavaScript SDK',
       description: (
         'A small JavaScript library that allows you to harness ' +
         'the power of Facebook, bringing the user\'s identity, ' +
         'social graph and distribution power to your site.'
       ),
       href: 'http://github.com/facebook/connect-js'
     },
     target_id: 'ENTER YOU FRIENDS IDS - more than one, seperate by commas',
     action_links: [
       { text: 'Code', href: 'http://github.com/facebook/connect-js' }
     ],
     user_message_prompt: 'Share your thoughts about Connect'
   },
   function(response) {
     if (response && response.post_id) {
       alert('Post was published.');
     } else {
       alert('Post was not published.');
     }
   }

 );

需要编辑或添加的行:target_id: '输入您的朋友 IDS - 多个,用逗号分隔',

:)

Here you go using javaScript SDK

STEP 1

At first, your application should obtain the permission for posting on user's wall or user's friends wall.

sample code from Facebook : link

FB.login(function(response) {
  if (response.session) {
    if (response.perms) {
      // user is logged in and granted some permissions.
      // perms is a comma separated list of granted permissions
    } else {
      // user is logged in, but did not grant any permissions
    }
  } else {
    // user is not logged in
  }
}, {perms:'read_stream,publish_stream,offline_access'});

publish_stream is required to post on user's or user's friends wall.

Line which one need to edit : {perms:'read_stream,publish_stream,offline_access'})

To read more about other permissions : link

STEP 2

Taken from facebook JavaScript SDK Pages and tweaked link

   FB.ui(
   {
     method: 'stream.publish',
     message: 'getting educated about Facebook Connect',
     attachment: {
       name: 'Connect',
       caption: 'The Facebook Connect JavaScript SDK',
       description: (
         'A small JavaScript library that allows you to harness ' +
         'the power of Facebook, bringing the user\'s identity, ' +
         'social graph and distribution power to your site.'
       ),
       href: 'http://github.com/facebook/connect-js'
     },
     target_id: 'ENTER YOU FRIENDS IDS - more than one, seperate by commas',
     action_links: [
       { text: 'Code', href: 'http://github.com/facebook/connect-js' }
     ],
     user_message_prompt: 'Share your thoughts about Connect'
   },
   function(response) {
     if (response && response.post_id) {
       alert('Post was published.');
     } else {
       alert('Post was not published.');
     }
   }

 );

Line which one needs to edit or add : target_id: 'ENTER YOU FRIENDS IDS - more than one, seperate by commas',

:)

木格 2024-10-10 02:21:49

您无法将 ajax 请求发送到另一个域。

只需使用 facebook JS SDK 并做任何你想做的事。

You cannot send ajax request to the another domain.

Just take facebook JS SDK and do whatever you want.

过气美图社 2024-10-10 02:21:49

我不确定我在哪里听到这个,但是:

jQuery.getJSON("https://graph.facebook.com/"+therequestid+"/?callback=?&access_token="+access_token,{},
           function(data, textStatus, jqXHR){
                          //bla-bla
           }
       );

应该可以,你需要在哪里添加“?callback =?”东西。我不知道为什么会这样。但是,只要它有效,我就会感到高兴。

I'm not sure where did I heard this, but:

jQuery.getJSON("https://graph.facebook.com/"+therequestid+"/?callback=?&access_token="+access_token,{},
           function(data, textStatus, jqXHR){
                          //bla-bla
           }
       );

Should work, where you need to add "?callback=?" stuff. I do not know why did this work. But, as long as it works, I'll stay happy.

淡墨 2024-10-10 02:21:49

您可能需要在对用户进行身份验证时设置范围(如果您尚未这样做)。

如果您使用 WAMS 进行身份验证,请检查此博客以设置范围 http://blogs.msdn.com/b/azuremobile/archive/2013/11/25/what-s-new-in-azure-mobile -services-1-6-4247.aspx

You may need to set the scope while authenticating the user, (if you haven't done so).

If you authenticated using WAMS, check this blog to set scopes http://blogs.msdn.com/b/azuremobile/archive/2013/11/25/what-s-new-in-azure-mobile-services-1-6-4247.aspx

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