简体   繁体   中英

New Facebook sdk: Issue accessing graph api

I am using the new facebook sdk in android to get the facebook albums. The code I am using is,

Session session = Session.getActiveSession();
Request request = new Request(null,
"https://graph.facebook.com/me/albums?access_token=" + session.getAccessToken());
Response response = Request.executeAndWait(request)

When I do a https://graph.facebook.com/me/albums?access_token=ACCESSTOKEN I see the valid Json .

The request object is,

{Request:  session: {Session state:OPENED_TOKEN_UPDATED, token:
{AccessToken token:ACCESSTOKEN permissions:[photo_upload, publish_stream, video_upload, share_item, installed, user_photos, status_update, create_note, publish_actions]},  
 appId:281846961912565}, graphPath: https://graph.facebook.com/me/albums?
access_token=ACCESSTOKEN, graphObject: null, restMethod: null, httpMethod: GET, parameters: Bundle[{migration_bundle=fbsdk:20121026}]}

But when I do a Response response = Request.executeAndWait(request); I get

{Response:  responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 190, errorType: OAuthException, errorMessage: Malformed access token ACCESSTOKEN?format=json}, isFromCache:false}

Complete code is as follows:

private class FetchUserPhotos extends AsyncTask<String, Integer, Boolean> {

    private ProgressDialog pd = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = ProgressDialog.show(MainActivity.this, "WWD",
                "Fetching photos..", true, true);
    }

    @Override
    protected Boolean doInBackground(String... params) {
        Session session = Session.getActiveSession();
        Request request = new Request(null,
                "https://graph.facebook.com/me/albums?access_token="
                        + session.getAccessToken());

        Response response = request.executeAndWait();
        return true;
    }
    @Override
    protected void onPostExecute(Boolean result) {
        pd.dismiss();
    }

}

Could any of you please tell me if there is something wrong in the way i am accessing it please?

Found the answer to this issue.The correct code is

        Session session = Session.getActiveSession();
        Request request = new Request(session, "me/albums");

        Response response = request.executeAndWait();

You don't have to specify the entire link and the accesstoken.

Declare this Async Task :

private class FBTask extends AsyncTask<Object, Object, Response> {        
    protected Response doInBackground(Object... arg0) {
        Request request = new Request(null,
                "https://graph.facebook.com/me/albums?access_token=" + session.getAccessToken());
        // Execute the request synchronously in the background
        // and return the response.
        return request.executeAndWait();
    }

    protected void onPostExecute(Response response) {
        // When the task completes, process
        // the response on the main thread
        onPostActionResponse(response);
    }

}

And call this task to execute :

new FBTask().execute(); 

Thanks.

//if session is opened

Bundle params=new Bundle();
params.putString("message",edittext.getText().toString());

Request mrequest=new Request(msession,postid+"/comments/?access_token="+msession.getAccessToken(),params,HttpMethod.POST,new Request.Callback() {


@Override
public void onCompleted(Response response) {
Log.d("RESPONSE",response.toString());
}});
mrequest.executeAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM