2014-09-10 56 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

相关问题