新的Facebook sdk:问题访问图api

时间:2013-01-22 16:38:56

标签: java android facebook facebook-graph-api

我在android中使用新的facebook sdk来获取facebook专辑。 我正在使用的代码是,

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

当我执行https://graph.facebook.com/me/albums?access_token=ACCESSTOKEN时,我会看到有效的Json

请求对象是,

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

但是当我做Response response = Request.executeAndWait(request);时,我得到了

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

完整代码如下:

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

}

请你告诉我,如果我访问它的方式有问题吗?

3 个答案:

答案 0 :(得分:4)

找到了这个问题的答案。正确的代码是

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

        Response response = request.executeAndWait();

您不必指定整个链接和accessstoken。

答案 1 :(得分:0)

声明此异步任务:

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

}

并调用此任务执行:

new FBTask().execute(); 

感谢。

答案 2 :(得分:0)

//如果会话被打开

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