Blogger JSON API添加帖子

时间:2012-08-14 08:22:39

标签: java android api request blogger

我想使用Blogger API将帖子添加到我的博客中。我成功获得了使用Blogger API的权利,并在Google API控制台中激活了它们。我使用this教程来获取access_token。我找到this question,所以在请求之前我获得了新的request_token。

当我第一次提出添加帖子的请求时,我收到了错误: 401“消息”:“凭据无效”,“位置”:“授权”

当我第二次请求添加带有新令牌的帖子时,我收到错误: 403“消息”:“超出每日限制。请注册”

我的请求代码是:

final JSONObject obj = new JSONObject();
obj.put("id", mUserID);

final JSONObject requestBody = new JSONObject();
requestBody.put("kind", "blogger#post");
requestBody.put("blog", obj);
requestBody.put("title", msg[0]);
requestBody.put("content", msg[0] + " " + msg[1]);

final HttpPost request = new HttpPost("https://www.googleapis.com/blogger/v3/blogs/" +   mUserID + "/posts");
request.addHeader("Authorization", "Bearer " + mToken);
request.addHeader("Content-Type", "application/json");
request.setEntity(new StringEntity(requestBody.toString()));
final HttpResponse response = mHttpClient.execute(request);

final HttpEntity ent = response.getEntity();
Log.i(SocialPoster.LOG, EntityUtils.toString(ent));
ent.consumeContent();

更新 找到解决方案:只需在请求的URL中添加“?key = {MY_API_KEY}”即可解决问题

1 个答案:

答案 0 :(得分:3)

您链接的教程网站

  

“API密钥是必需的,因为它标识了您的应用程序,因此允许API扣除配额并使用为您的项目定义的配额规则。您需要在任务服务对象上指定API密钥。”

useTasksAPI(String accessToken) {
  // Setting up the Tasks API Service
  HttpTransport transport = AndroidHttp.newCompatibleTransport();
  AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessToken);
  Tasks service = new Tasks(transport, accessProtectedResource, new JacksonFactory());
  service.accessKey = INSERT_YOUR_API_KEY;
  service.setApplicationName("Google-TasksSample/1.0");

  // TODO: now use the service to query the Tasks API
}

听起来像是你错过了API密钥,使用错误,在代码中放错了地方或者以错误的方式将其提供给服务。

我没有在这里查看代码,但这是Google的示例代码,用于您要执行的操作。使用this code.

测试您的API密钥