簡體   English   中英

新的Facebook sdk:問題訪問圖api

[英]New Facebook sdk: Issue accessing 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();
    }

}

你能不能告訴我,如果我訪問它的方式有問題嗎?

找到了這個問題的答案。正確的代碼是

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

        Response response = request.executeAndWait();

您不必指定整個鏈接和accesstoken。

聲明此異步任務:

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

謝謝。

//如果會話被打開

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM