考拉宝石和从朋友那里获取相册照片

时间:2014-06-19 19:57:05

标签: ruby-on-rails facebook-graph-api facebook-fql omniauth koala

我正在创建一个facebook应用程序。我使用 omniauth-facebook gem登录用户。我问以下权限:

provider :facebook, 'app_id', 'secret_key',
scope: "public_profile,user_friends,friends_photos,user_photos"

用户登录后,我正在接待他的朋友'带有koala gem的列表,类似于以下代码:

@graph = Koala::Facebook::API.new(current_user.oauth_token)
@profile = @graph.get_object("me")
@friends = @graph.get_connections("me", "friends?fields=id,name,gender")

我不知道我是否可以为他的每个朋友获取相册和照片。我已经尝试了以下fql查询:

SELECT pid FROM photo WHERE aid IN (SELECT aid,name FROM album WHERE owner=user_id)

当我在图形api资源管理器上测试它时,它只适用于我,但它不适用于我的应用程序(既不适合我的朋友也不适合我)。

我是否错过了某些权限,或者无法获取某人朋友的照片和相册?有关通过脸谱图API获取某人朋友的所有照片的其他建议吗?

更新

我已在此处上传了代码https://github.com/johndel/fbgame 你可以在 config / initializers / omniauth.rb app / controllers / pages_controller.rb

上看到上面的代码及其余部分。

1 个答案:

答案 0 :(得分:2)

问题与您的应用在注册期间提出的权限有关。如果您查看它,它不会要求您的朋友的照片。

以上意味着问题与omniauth-facebook gem有关,因为这就是你正在使用的。您使用的是旧版本(版本1.4.0),即使我更新了它仍然无法使用当前版本(1.6.0)。

我使用了javascript版本,它正在生成一个正确的omniauth键并询问更好的权限(朋友的照片访问权限)。登录后,它与考拉宝石很好地配合,所以你可能必须通过其他方法获得omniauth密钥,因为当前的omniauth-facebook宝石似乎有一些bug(我认为javascript方法是我'使用过的,是建议的方法)。

这是带有javascript的代码(大部分是来自facebook图api的复制粘贴):

<!DOCTYPE html>
<html>
  <head>
    <title>Facebook Login JavaScript Example</title>
    <meta charset="UTF-8">
  </head>
  <body>
    <div style="width: 900px; margin: 0 auto;">

      <script>
        // This is called with the results from from FB.getLoginStatus().
        function statusChangeCallback(response) {
          // The response object is returned with a status field that lets the
          // app know the current login status of the person.
          // Full docs on the response object can be found in the documentation
          // for FB.getLoginStatus().
          if (response.status === 'connected') {
            // Logged into your app and Facebook.
            testAPI();
          } else if (response.status === 'not_authorized') {
            // The person is logged into Facebook, but not your app.
            document.getElementById('status').innerHTML = 'Please log ' +
              'into this app.';
          } else {
            // The person is not logged into Facebook, so we're not sure if
            // they are logged into this app or not.
            document.getElementById('status').innerHTML = 'Please log ' +
              'into Facebook.';
          }
        }

        // This function is called when someone finishes with the Login
        // Button.  See the onlogin handler attached to it in the sample
        // code below.
        function checkLoginState() {
          FB.getLoginStatus(function(response) {
            statusChangeCallback(response);
          });
        }

        window.fbAsyncInit = function() {
        FB.init({
          appId      : 'api_key',
          cookie     : true,  // enable cookies to allow the server to access
                              // the session
          xfbml      : true,  // parse social plugins on this page
          version    : 'v2.0' // use version 2.0
        });

        // Now that we've initialized the JavaScript SDK, we call
        // FB.getLoginStatus().  This function gets the state of the
        // person visiting this page and can return one of three states to
        // the callback you provide.  They can be:
        //
        // 1. Logged into your app ('connected')
        // 2. Logged into Facebook, but not your app ('not_authorized')
        // 3. Not logged into Facebook and can't tell if they are logged into
        //    your app or not.
        //
        // These three cases are handled in the callback function.

        FB.getLoginStatus(function(response) {
          statusChangeCallback(response);
        });

        };

        // 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/sdk.js";
          fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));

        // Here we run a very simple test of the Graph API after login is
        // successful.  See statusChangeCallback() for when this call is made.
        function testAPI() {
          FB.api('/me/albums', function(response) {
            console.log(JSON.stringify(response));
            // document.getElementById('status').innerHTML =
            //   'Thanks for logging in, ' + response.name + '!';
          });

        }
      </script>

      <!--
        Below we include the Login Button social plugin. This button uses
        the JavaScript SDK to present a graphical Login button that triggers
        the FB.login() function when clicked.
      -->





      <fb:login-button scope="public_profile,user_friends,friends_photos,user_photos" onlogin="checkLoginState();">
      </fb:login-button>

      <div id="fb-root">
      </div>


      <div id="status">
      </div>


      <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

      <script>
      $(function() {
        // FB.api('/me', function(response) {
        //   console.log(JSON.stringify(response));
        // });
      });
      </script>
    </div>
  </body>
</html>

成功生成具有正确权限的omniauth密钥后,它可以正常工作。你可以用考拉或javascript方式测试它,就像我通过调用上面的方法一样。