如何使用fb_graph邀请来自Facebook的朋友?

4
基本上我想显示一个好友列表(包括图片和姓名),并邀请他们加入我正在使用fb_graph gem 创建的 Rails 应用程序。链接展示了此工作流程,您将单击“邀请好友”按钮,然后会弹出好友列表,其中有一个按钮可让您邀请相应的用户。

http://www.quora.com/User-Acquisition/What-is-the-best-invite-a-friend-flow-for-mobile

是否有一种方法可以在fb_graph中实现这一点?


类似这样的吗?https://developers.facebook.com/docs/guides/games/custom-muti-friend-selector/ - CBroe
是的,但理想情况下我想使用 fb_graph 宝石。 - locoboy
4个回答

9

没问题。假设您已经有了一个可运行的fb_graph实现,您可以使用以下命令(来自https://github.com/nov/fb_graph)获取好友列表:

FbGraph::User.me(fb_token).friends

您可以使用这个来生成您的好友列表UI,然后邀请选定的好友,就像这样(未经测试的修改来自前面的链接,以及https://developers.facebook.com/docs/reference/dialogs/requests/):

app_request = FbGraph::User.me(token).app_request!(
  :message => 'invitation message',
  :to      => friend.uid
)

前面的代码也可以接受逗号分隔的UID集合。

我会让你设计和编写用户界面,但使用fb_graph肯定是可能的。如果你决定扩大范围,这两个链接应该是非常有用的。


1
这对我没用。看起来Facebook不允许通过此方法进行请求。所有请求最终都会发送给发出请求的用户。 - Paul

3
感谢您对我的宝石的兴趣。 Brad Werth的代码适用于现有用户(已安装您的应用程序),但可能不适用于新用户。
有很多限制可以在后台发送应用程序请求,以避免垃圾邮件。这种限制直接影响fb_graph的发送应用程序请求功能。
如果您正在开发iOS应用程序,则建议您使用FB官方iOS SDK。 https://developers.facebook.com/docs/howtos/send-requests-using-ios-sdk/ 使用iOS SDK(或html5应用程序中的JS SDK),您的限制较少。
附注。 我不熟悉Android应用程序开发或FB官方Android SDK,但我假设它们在其Android SDK中也具有类似的功能。

我实际上正在使用HTML5和Rails进行开发。根据@iioiooioo的说法,这个gem有没有我不知道的限制?此外,浏览这个gem功能的最佳方法是什么? - locoboy
官方文档中提到:“注意:向 /USER_ID/apprequests 的 Graph API 端点 POST 是被视为应用程序向用户的请求。与使用请求对话框发送的用户之间的请求相比,应用程序生成的请求不会收到通知并且分发受到限制。”@iioiooioo 的示例代码中,“to” 参数无效。 “to” 只能作为响应参数而不是请求参数。 - nov matake

1
这可能会有所帮助,我在我的PHP服务器上使用它并且它有效。
这段代码可以帮助您发布、发送消息,并向您的朋友发送请求。 头标签:
<script type="text/javascript">
  function logResponse(response) {
    if (console && console.log) {
      console.log('The response was', response);
    }
  }

  $(function(){
    // Set up so we handle click on the buttons
    $('#postToWall').click(function() {
      FB.ui(
        {
          method : 'feed',
          link   : $(this).attr('data-url')
        },
        function (response) {
          // If response is null the user canceled the dialog
          if (response != null) {
            logResponse(response);
          }
        }
      );
    });

    $('#sendToFriends').click(function() {
      FB.ui(
        {
          method : 'send',
          link   : $(this).attr('data-url')
        },
        function (response) {
          // If response is null the user canceled the dialog
          if (response != null) {
            logResponse(response);
          }
        }
      );
    });

    $('#sendRequest').click(function() {
      FB.ui(
        {
          method  : 'apprequests',
          message : $(this).attr('data-message')
        },
        function (response) {
          // If response is null the user canceled the dialog
          if (response != null) {
            logResponse(response);
          }
        }
      );
    });
  });
</script>

正文标签:

<body>
<div id="fb-root"></div>
<script type="text/javascript">
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'xxxxxxxxxxxxxxxxxxx', // App ID
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true // parse XFBML
    });

    // Listen to the auth.login which will be called when the user logs in
    // using the Login button
    FB.Event.subscribe('auth.login', function(response) {
      // We want to reload the page now so PHP can read the cookie that the
      // Javascript SDK sat. But we don't want to use
      // window.location.reload() because if this is in a canvas there was a
      // post made to this page and a reload will trigger a message to the
      // user asking if they want to send data again.
      window.location = window.location;
    });

    FB.Canvas.setAutoGrow();
  };

  // Load the SDK Asynchronously
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
</script>

<header class="clearfix">
  <?php if (isset($basic)) { ?>
  <p id="picture" style="background-image: url(https://graph.facebook.com/<?php echo he($user_id); ?>/picture?type=normal)"></p>

  <div>
    <h1>Welcome, <strong><?php echo he(idx($basic, 'name')); ?></strong></h1>
    <p class="tagline">
      This is your app
      <a href="<?php echo he(idx($app_info, 'link'));?>" target="_top"><?php echo he($app_name); ?></a>
    </p>

    <div id="share-app">
      <p>Share your app:</p>
      <ul>
        <li>
          <a href="#" class="facebook-button" id="postToWall" data-url="<?php echo AppInfo::getUrl(); ?>">
            <span class="plus">Post to Wall</span>
          </a>
        </li>
        <li>
          <a href="#" class="facebook-button speech-bubble" id="sendToFriends" data-url="<?php echo AppInfo::getUrl(); ?>">
            <span class="speech-bubble">Send Message</span>
          </a>
        </li>
        <li>
          <a href="#" class="facebook-button apprequests" id="sendRequest" data-message="Test this awesome app">
            <span class="apprequests">Send Requests</span>
          </a>
        </li>
      </ul>
    </div>
  </div>
  <?php } else { ?>
  <div>
    <h1>Welcome</h1>
    <div class="fb-login-button" data-scope="user_likes,user_photos"></div>
  </div>
  <?php } ?>
</header>

1
当然,Facebook提供了一个漂亮的对话框来选择您想要发送请求的朋友。但是还有一个很好的工具,内置JavaScript,通过它您可以获得与Facebook Friends选择器对话框相同类型的工具。 JQuery Facebook Multi-Friend Selector Plugin 此插件将向Facebook发出Graph API调用并收集您的好友列表。此插件的优点在于它将以“懒加载模式”加载所有好友列表。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接