BamsBamx
BamsBamx

Reputation: 4256

Android - Get profile cover picture from Facebook

I am using Facebook SDK and Parse SDK and I want to retrieve the profile cover picture.

I am doing the following:

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

But I am not able to get the proper response since it says I need an access token (the user has already been logged in).

{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
}

Is there any fix for this available?

Upvotes: 2

Views: 1711

Answers (2)

AhabLives
AhabLives

Reputation: 1468

Facebook changed a few things and has some terrible documentation. Hope this helps someone else it's what worked for me.

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();
}

Upvotes: 0

BamsBamx
BamsBamx

Reputation: 4256

After spending A LOT of hours searching for the answer, I finally got it !!!

The Android SDK for Facebook docs, are too useless.

To solve this problem we just need to set the Graph Path in the second param and a Bundle with fields as third param. Example:

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();

Then we can parse the response object returned in onCompleted as JSON with

response.getGraphObject().getInnerJsonObject();
//or
response.getGraphObject().getProperty("cover");

Source: New Facebook SDK and OAuthException in Graphpath requests thanks to @Jesse Chen

Upvotes: 2

Related Questions