1

我在 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)

当我执行 a 时,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}]}

但是当我做 aResponse 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();
    }

}

你们中的任何人都可以告诉我我访问它的方式是否有问题吗?

4

3 回答 3

4

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

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

        Response response = request.executeAndWait();

您不必指定整个链接和访问令牌。

于 2013-01-22T19:03:02.827 回答
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(); 

谢谢。

于 2013-01-22T17:01:14.607 回答
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();
于 2013-05-27T08:53:43.013 回答