如何在Android中从Facebook获取所有照片 [英] How to fetch all photos from Facebook in Android

查看:179
本文介绍了如何在Android中从Facebook获取所有照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中出于不同的目的使用Facebook SDK.目前,我已经通过Facebook实现了登录.现在,我的下一个任务是获取已登录用户的所有照片,并在网格中显示所有照片.我正在关注官方的Facebook开发人员网站,以作为此任务的参考.以下是我用来获取用户照片的代码.

I am developing an application in which I am using Facebook SDK for different purposes. Currently I have implemented Login through Facebook. Now my next task is to fetch all the photos of the logged in user and display all of them in a grid. I am following the official facebook developer website as a reference for this task. Following is the code that I am using to fetch the user photos.

    new Request(
            Session.getActiveSession(),
            "/me/photos",
            getRequestParameters(),
            HttpMethod.GET,
            new Request.Callback() {
                public void onCompleted(Response response) {
                    /* handle the result */
                    Log.d("Response ","Response " + response);
                    mpBar.setVisibility(View.GONE);
                }
            }
        ).executeAsync();

private Bundle getRequestParameters() 
    {
        String accessToken = LoginSigbUpCustomerNew.mytoken;
           Bundle parameters = new Bundle(1);
           parameters.putString("access_token", accessToken);
           return parameters;
    }

在这里,我得到这样的答复:-

Here, I am getting response like this :-

{Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[]}}, error: null, isFromCache:false}

我不明白为什么数据数组为空.另外,我正在请求用户的user_photos权限.所以哪里出了问题. 我是这个Facebook SDK的新手,不知道如何解决这个问题.如果有人可以帮助我,那就太好了.

I cant understand why the data array is empty. Also I am requesting the user_photos permission from user. So where the thing is wrong. I am new to this Facebook SDK and dont know how to solve this. If anyone can help me it would be great.

谢谢.

推荐答案

根据Facebook SDK的文档,用于请求照片的代码就可以了,但是我认为您忘记了请求登录用户的许可(您可以在这种情况下).

Your code to request your photos is just fine according to the documentation of Facebook SDK however I think that you forgot to request permission from the user who's logged in (you in this case).

从Facebook SDK文档复制:A user access token with user_photos permission is required to see all photos that person is tagged in or has uploaded.

Copied from Facebook SDK documentation: A user access token with user_photos permission is required to see all photos that person is tagged in or has uploaded.

以下链接为您提供了有关如何向登录按钮添加访问令牌的小型教程:

The following link gives you a small tutorial on how to add access tokens to the login button: https://developers.facebook.com/docs/android/login-with-facebook/v2.2?locale=en_GB#step3

以您的情况为准(代码段仅适用于活动,适用于片段:单击上面的url并按照那里的教程进行操作.)

In your case it would be (code snippet is for activities only, for fragments: click the url above and follow the tutorial there. Pretty straight forward):

...
LoginButton authButton = (LoginButton) findViewById(R.id.ID_OF_YOUR_LOGIN_BUTTON);
authButton.setReadPermissions(Arrays.asList("user_photos"));
...

在您的重写onCreate方法中.

In your overriding onCreate method.

方法二:

...
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
    session.openForRead(new Session.OpenRequest(this)
        .setPermissions(Arrays.asList("user_photos"))
        .setCallback(YOUR_CALLBACK_CLASS_VARIABLE));
} else {
    // Open an active session. Basically happens when someone is already logged in.
}
...

在拨打电话以请求所有用户照片之前,您可以在用户尚未登录时简单地打开一个新请求.这可能发生在ButtonView.OnClickListener类中,因为您似乎不是Facebook的LoginButton.

Before making the call to request all the user photos, you can simply open a new request when the user hasn't logged in yet. This can happen in View.OnClickListener class of a Button as it seems that you're not LoginButton of Facebook.

YOUR_CALLBACK_CLASS_VARIABLE是实现Session.StatusCallback的类.我敢肯定,你知道它是什么,它是做什么的.

YOUR_CALLBACK_CLASS_VARIABLE is your class implementing Session.StatusCallback. I'm quite certain you know what it is and what it does.

方法三:

...
    Session.NewPermissionsRequest newPermissionsRequest = new Session
      .NewPermissionsRequest(this, Arrays.asList("user_photos"));
    session.requestNewReadPermissions(newPermissionsRequest);
...

当用户已经登录时(在用户登录后),并且您希望向用户请求其他权限以访问其照片或任何其他内容.顺便说一下,这都是在Android版Facebook SDK的文档中编写的.

When the user is already logged in (so after the user is logged in) and you wish to request additional permissions from the user to access his photos or whatsoever. This is by the way all written in the docs of Facebook SDK for Android.

有关更多信息: https: //developers.facebook.com/docs/android/login-with-facebook/v2.2?locale=en_GB#step3

这篇关于如何在Android中从Facebook获取所有照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆