如何使用fb_graph邀请facebook上的朋友?

时间:2012-08-29 00:44:56

标签: ruby-on-rails facebook fb-graph

基本上我想显示一个朋友列表(带有图片和名称),并邀请他们加入我的应用程序,我正在使用fb_graph gem在rails中创建。该链接显示了这将如何工作的流程,基本上你会点击“邀请朋友”按钮,然后会弹出一个按钮,允许你邀请相应的用户。

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

有没有办法用fb_graph执行此操作?

4 个答案:

答案 0 :(得分:9)

你打赌,打赌。假设您有一个fb_graph的工作实现,您可以使用以下命令(来自https://github.com/nov/fb_graph)获取朋友列表:

FbGraph::User.me(fb_token).friends

您可以使用它为您的朋友列表生成您的用户界面,然后邀请所选的朋友,如上所述(来自上一个链接的未经测试的修改,以及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 :(得分:3)

感谢您对我宝石的兴趣。

Brad Werth's code适用于现有(=已安装的应用)用户,但可能不适用于新用户。

在后台发送应用请求以避免发送垃圾邮件有很多限制。 该限制直接影响fb_graph的发送应用请求功能。

如果您正在开发iOS应用程序,我建议您使用FB官方iOS SDK。 https://developers.facebook.com/docs/howtos/send-requests-using-ios-sdk/

使用iOS SDK(或html5应用程序中的JS SDK),您的限制较少。

PS。 我不熟悉Android App开发或FB官方Android SDK,但我认为他们的Android SDK也有类似的功能。

答案 2 :(得分:1)

当然Facebook提供了一个漂亮的对话框来选择你想要发送请求的朋友。但是还有一个很好的工具,内置在javascript中,通过它你可以拥有相同类型的Facebook好友选择器对话框。

JQuery Facebook Multi-Friend Selector Plugin

此插件将对Facebook进行Graph API调用并收集您的好友列表。这个插件的优点是它将以“延迟加载模式”加载所有好友列表。

答案 3 :(得分: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>
相关问题