2014-09-10 64 views
0

我試圖獲取Android應用程序的內部博客帖子的JSON名單,但我不斷收到這個JSON作爲結果:的Blogger API調用返回401錯誤的Android

{ 
"error": { 
"errors": [ 
{ 
"domain": "global", 
"reason": "required", 
"message": "Login Required", 
"locationType": "header", 
"location": "Authorization" 
} 
], 
"code": 401, 
"message": "Login Required" 
} 
} 

這裏的格式我api電話。

https://www.googleapis.com/blogger/v3/blogs/BLOG_ID/posts?key=API_KEY

隨着BLOG_ID和API_KEY是佔位符代碼中使用的實際值。

這裏是檢索JSON的代碼。

private class AsyncCaller extends AsyncTask<Void, Void, String> { 

    @Override 
    protected String doInBackground(Void... params) { 
     // TODO Auto-generated method stub 



     DefaultHttpClient httpclient = new DefaultHttpClient(
       new BasicHttpParams()); 
     HttpPost httppost = new HttpPost(Constants.BLOOGER_API_REQUEST_PREFIX 
       + "/blogs/" + Constants.BLOGGER_TEST_BLOGID + "/posts?key=" 
       + Constants.GOOGLE_API_KEY); 

     Log.d("HTTPPOST", Constants.BLOOGER_API_REQUEST_PREFIX 
       + "/blogs/" + Constants.BLOGGER_TEST_BLOGID + "/posts?key=" 
       + Constants.GOOGLE_API_KEY); 

     httppost.setHeader("Content-type", "application/json"); 

     InputStream inputStream = null; 
     String result = ""; 
     Log.d("BEFORE TRY", "BEFORE TRY"); 
     try { 
      HttpResponse response = httpclient.execute(httppost); 
      Log.d("Executed HTTP POST", "EXECUTED HTTPPOST"); 
      HttpEntity entity = response.getEntity(); 
      Log.d("GOT ENTITY", "GOT ENTITY"); 
      inputStream = entity.getContent(); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(
        inputStream, "UTF-8"), 8); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 

      Log.d("INSIDE TRY", "INSIDE TRY"); 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
       Log.d("IN LOOP", "IN LOOP"); 
      } 
      result = sb.toString(); 

      Log.d("RESULT DONE", result.toString()); 


     } catch (Exception e) { 
      // Bad 
      Log.d("BAD E", "BAD E"); 
      e.printStackTrace(); 

     } finally { 

      try { 
       if (inputStream != null) { 
        inputStream.close(); 
       } 
      } catch (Exception squish) { 
      } 

     } 

     if(result != null){ 

     Log.d("RESULT GOOD", result.toString()); 
     } else { 
      Log.wtf("BAD", "NULL RESULT"); 

     } 

     onPostExecute(result); 
    return result; 
} 

正如您所看到的,我正在使用API​​密鑰簽署我的請求,所以這不是問題。

我已進入問題設置中的博客並將公開的可見性設置爲所以我認爲問題不是OAuth。我不清楚還有什麼需要在應用程序中實現「需要登錄」,我寧願如果用戶能夠打開應用程序並看到博客帖子,而不會暴露於引擎蓋下發生的事情。 而我的https,api調用在瀏覽器中工作得很好,給了我期待的結果的確切列表,所以API調用的語法似乎也不是問題,這給我帶來了相當大的損失。

+0

您是否在瀏覽器嘗試的PostMethod?你是否得到了預期的結果 – 2014-09-10 17:02:29

+0

@Indra它在瀏覽器中工作得很好。 – ChristianCuevas 2014-09-10 17:03:20

+0

其獲取方法或發佈方法 – 2014-09-10 17:11:57

回答

1

使用HTTPGET方法

 HttpGet httpGet = new HttpGet("https://www.googleapis.com/blogger/v3/blogs/BLOG_ID/posts?key=" +paramAPIKEY); 

只需更換與獲取

+0

我誤解了你以前的評論,我很抱歉,謝謝你的幫助。 – ChristianCuevas 2014-09-10 17:22:23

+0

有一個快樂的編碼:) – 2014-09-10 17:23:00

相關問題