Blogger API调用返回401错误Android

时间:2014-09-10 16:53:38

标签: android json blogger

我正在尝试在Android应用内部检索Blogger帖子的JSON列表,但我不断收到此JSON:

{
"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。我并不知道还需要做什么才能实现&#34;需要登录&#34;在应用程序中,我更希望用户能够打开应用程序并查看博客帖子,而不会暴露于幕后发生的事情。 我的https,api调用在浏览器中运行得非常好,为我提供了我期待的结果的确切列表,因此API调用的语法似乎也不是问题,这让我失望非常亏本。

1 个答案:

答案 0 :(得分:1)

使用httpGet方法

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

只需用Get

替换postmethod