【问题标题】:Blogger JSON API add a postBlogger JSON API 添加帖子
【发布时间】:2012-08-14 08:22:38
【问题描述】:

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

当我第一次请求添加帖子时,我收到错误消息:401 "message": "Invalid Credentials", "location": "Authorization"

当我第二次请求使用新令牌添加帖子时,我收到错误:403 "message": "Daily Limit Exceeded. Please sign up"

我的请求代码是:

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}”即可解决问题

【问题讨论】:

  • 您的问题对我帮助很大,衷心感谢您的提问和解答
  • 你使用的是 DefaultHttpClient 还是 HttpClient

标签: java android api request blogger


【解决方案1】:

您链接的教程网站说明

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

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 密钥

【讨论】:

  • 感谢您的回答,我尝试将“?key=”添加到请求的 url 并且有效 =)
  • 太棒了,很高兴为您服务:)
猜你喜欢
  • 1970-01-01
  • 2017-07-25
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多