从Graph API v2.4开始,对用户ID /相册的GET请求应该包含一个明确的"字段"参数

时间:2015-09-16 10:24:56

标签: android

我成功登录使用Graph Api V 2.4现在我试图从Facebook上获取所有专辑图片但是卡在这里,任何帮助都可以适用

protected void getAlbumPics() {
    /* UserID=872269919494274 */
    new GraphRequest(AccessToken.getCurrentAccessToken(),
        "/{872269919494274}/albums", null, HttpMethod.GET, new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                /* handle the result */
                //?fields=photos
                if (response.getError() != null) {
                    // handle error
                    System.out.println("ERROR");
                } else {
                    System.out.println("Success response== "+response);
                }
            }
        }
    ).executeAsync();

}

3 个答案:

答案 0 :(得分:1)

首先您需要在Facebook按钮上添加一些权限,以获取您可以使用以下代码行的用户图像:

loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends,user_photos"));

然后在您的图表API中添加以下代码...

 JSONObject picture= (data.getJSONObject("picture"));

这是完整的代码:

    GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                try {  // Application code
                                    JSONObject albums=new JSONObject(object.getString("albums"));
                                    Log.d("JSONALBUM ", ""+albums);


JSONArray data = (albums.getJSONArray("data"));
                                        Log.d("JSONDATA ", "" + data);

                                        for (int i = 0; i < data.length(); i++){
                                            JSONObject jsonObjectPictures=data.getJSONObject(i);

//final image is String array
                     finalImage[i]=jsonObjectPictures.getString("pictures");

                                     }

                                    JSONObject picture= (data.getJSONObject("picture"));
                                    Log.d("JSONPicturea ", ""+picture);

                                    String e = object.getString("email");
                                    Log.d("Email ", e);


                                } catch (JSONException E) {
                                    E.printStackTrace();
                                }
                                Log.e("JSONObject", "" + object);
                                Log.e("GraphResponse", "" + response);
                                Log.e("LoginActivity", response.toString());
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday, friends,albums");
                request.setParameters(parameters);
                request.executeAsync();

                startActivity(i);

             Intent i = new Intent(LoginActivity.this, MainActivity.class);


            }

答案 1 :(得分:1)

我试过这个方法来获取相册图片,成功只获取相册图片ID,创建时间不是链接或图片网址,这里是我的代码

    private void getAlbum_pics(ArrayList<String> Album_id_list) {
    Bundle params = new Bundle();

    params.putBoolean("redirect", false);
    params.putString("height", "400");
    params.putString("type", "small");
    params.putString("width", "400");
    params.putString("link", "link");

    new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + Album_id_list.get(0)
            + "/photos", params, HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {

                    JSONObject object = response.getJSONObject();

                    try {
                        JSONArray data_array1 = object.getJSONArray("data");
                        for (int i = 0; i < data_array1.length(); i++) {
                            JSONObject _pubKey = data_array1
                                    .getJSONObject(i);
                            String arrayfinal = _pubKey.getString("id");
                            Log.d("pic ID ==  ", "" + arrayfinal);
                            Photo_list_id.add(arrayfinal);

                        }

                        getAlbumPictures(Photo_list_id);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }).executeAsync();

}



here is method to get pics from id

    public void getAlbumPictures(ArrayList<String> newpiclist) {
    /* make the API call */
    Bundle params = new Bundle();
    params.putString("link", "link");
    params.putString("fieldname", "from");
    params.putString("type", "large");

    new GraphRequest(AccessToken.getCurrentAccessToken(), "/"
            + newpiclist.get(0), params, HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {

                    JSONObject newpicobject = response.getJSONObject();
                    Log.e("pics photo idss==", newpicobject.toString());
                }
            }).executeAsync();

}

答案 2 :(得分:0)

YOYO我做了它让我检查上面我的错误是我没有在Query中添加字段所以新图Api V2.4中的朋友不要忘记设置如下演示字段

GraphRequest request = GraphRequest.newGraphPathRequest(
            AccessToken.getCurrentAccessToken(),
              "/" + album_pic_ids,
              new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {
                    try {
                        JSONObject newpicobject = response.getJSONObject();
                        Log.e("pics photo idss==", newpicobject + "");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "link,can_tag");
            request.setParameters(parameters);
            request.executeAsync();