Android - 从Facebook获取个人资料封面图片

时间:2014-08-13 22:46:40

标签: android facebook image sdk parse-platform

我正在使用Facebook SDK和Parse SDK,我想检索个人资料封面图片。

我正在做以下事情:

new Request(
        ParseFacebookUtils.getSession(),
        "/me?fields=cover",
        null,
        HttpMethod.GET,
        new Request.Callback() {
                public void onCompleted(Response response) {
                            Log.wtf("TAG",
                            response.toString());
                }
}).executeAsync();

但我无法得到正确的回复,因为它说我需要一个访问令牌(用户已经登录)。

{Response:  
    responseCode: 400, 
    graphObject: null, 
    error: {
        HttpStatus: 400, 
        errorCode: 2500, 
        errorType: OAuthException, 
        errorMessage: An active access token must be used to query information about the current user.
    }, 
    isFromCache:false
}

有没有可用的修复程序?

2 个答案:

答案 0 :(得分:2)

花了很多时间寻找答案后,我终于明白了!

用于Facebook文档的Android SDK太没用了。

要解决这个问题,我们只需要在第二个参数中设置图形路径,使用字段作为第三个参数设置Bundle。例如:

Bundle params = new Bundle();
            params.putString("fields", "cover");
            new Request(ParseFacebookUtils.getSession(),
                    "me",
                    params,
                    HttpMethod.GET,
                    new Request.Callback() {
                        @Override
                        public void onCompleted(Response response) {
                            //code...
                        }
                    }).executeAsync();

然后我们可以使用

将onCompleted中返回的响应对象解析为JSON
response.getGraphObject().getInnerJsonObject();
//or
response.getGraphObject().getProperty("cover");

来源:New Facebook SDK and OAuthException in Graphpath requests感谢@Jesse Chen

答案 1 :(得分:0)

Facebook改变了一些事情并且有一些糟糕的文档。希望这能帮助别人,这对我有用。

public void getCoverPhotoFB(final String email, AccessToken accessToken){

    if(!AccessToken.getCurrentAccessToken().getPermissions().contains("user_photos")) {
        Log.e(L, "getCoverPhotoFB....get user_photo permission");
        LoginManager.getInstance().logInWithReadPermissions(
                this,
                Arrays.asList("user_photos"));
    }

    ////

    Bundle params = new Bundle();
    params.putBoolean("redirect", false);
    params.putString("fields", "cover");
new GraphRequest(
        accessToken,
        "me",
        params,
        HttpMethod.GET,
        new GraphRequest.Callback() {
            public void onCompleted(final GraphResponse response) {
                Log.e(L, "getCoverPhotoFB..."+response);

                // thread is necessary for network call
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {

                            String picUrlString = (String) response.getJSONObject().getJSONObject("cover").get("source");
                            Log.d(L,"getCoverPhotoFB.....picURLString....."+picUrlString);
                            URL img_value = new URL(picUrlString);
                            Bitmap eventBitmap = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
                            saveImageToExternalStorage(eventBitmap, email + "_B.png");


                            homeProfile(profile, email);


                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });
                thread.start();
                }

            }
    ).executeAsync();
}