2013-03-12 57 views
2

這是我的第一個話題,我希望你能幫助我(請用我的英語很好,因爲我的第一語言是法語)THX :))獲得一個應用程序訪問令牌與Android FacebookSDK V3

我有一個問題,我試圖通過Facebook Android Facebook SDK v3獲得應用程序訪問令牌,但迄今爲止我還沒有成功。

我已經看到了互聯網上,讓應用程序令牌,我們需要訪問該鏈接:

https://graph.facebook.com/oauth/access_token client_id=*************&client_secret=***********************&%20grant_type=client_credentials 

所以我做了一個請求對象來獲取應用程序令牌:

Bundle bundle = new Bundle(); 
bundle.putString("client_id", appId); 
bundle.putString("client_secret", appSecret); 
bundle.putString("grant_type", "client_credentials"); 

Request request = new Request(null, "oauth/access_token", bundle, HttpMethod.GET); 
Response resp = request.executeAndWait(); 

該對象的「響應」返回給我一個GraphObject,如下所示: GraphObject{graphObjectClass=GraphObject, state={"FACEBOOK_NON_JSON_RESULT":"access_token"}}

但訪問令牌不包含在由請求者返回的響應中ST。

然而,當我啓動在瀏覽器中的鏈接我得到這樣一個頁面: access_token=[the_access_token]

爲什麼我不能得到蒙山的響應是一回事嗎? 我做錯了什麼?有沒有人有辦法解決嗎?

非常感謝!

+0

我認爲的事實,這個端點的反應是_not_ JSON可能是問題 - 如果您的請求對象希望它是JSON ,但得到別的東西,它可能會失敗。消息FACEBOOK_NON_JSON_RESULT也表明了這一點。 – CBroe 2013-03-12 16:14:51

回答

-1

我有同樣的問題。

訪問令牌不是JSON類型。所以你不能使用facebook api的'request'和'response'。 你將有鏈接下的解決方案。

Show this link

或在代碼中看到


public static void getAppAccessToken(){ 
    String url = "https://graph.facebook.com/oauth/access_token?client_id="+CLIENT_ID+"&client_secret="+CLIENT_SECRET+"&grant_type=client_credentials"; 
    InputStream is; 
    String result; 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet(url); 

    try{ 
     HttpResponse response = httpclient.execute(httpget); 

     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while ((line = reader.readLine()) != null) { 
      sb.append(line); 
     } 

     is.close(); 
     result=sb.toString(); 
     result = result.split("=")[1]; 
     appAccessToken = result; 
    } 
    catch(Exception e) 
    { 

    } 
} 

+0

從不討論你在其他人的問題,紅色的常見問題解答以獲得更好的理解,如果你回答這個問題,請提及它。 – Hamad 2013-11-21 08:23:30

相關問題