java - Blogger JSON API 添加帖子

标签 java android api request blogger

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

当我第一次请求添加帖子时,出现错误:401 "message": "Invalid Credentials", "location": "Authorization"

当我第二次请求使用新 token 添加帖子时,出现错误:403“message”:“超出每日限制。请注册”

我的请求代码是:

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

最佳答案

您链接的教程网站状态

"The API Key is mandatory as it identifies your application and therefore allows the API to deduct quota and use the quota rules defined for your project. You need to specify the API Key on your Tasks service Object."

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 key 、使用错误、将其错误地放置在代码中或以错误的方式将其提供给服务。

我还没有查看这里的代码,但这是 Google 的示例代码,说明您正在尝试执行的操作。使用 this code. 测试您的 API key

关于java - Blogger JSON API 添加帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11948420/

相关文章:

java - API 请求只能从 1 个 IP 地址发出

java - 为什么要递归地获取(可重入)锁?

java - 公共(public) IP 地址更改时本地主机套接字是否受到影响

Android 网络服务发现 "timeout"?

api - 有没有办法列出 TDB 存储中包含的模型?

java - 如何使用可分页参数定义请求方向?

java - 如何为类(class)生成随机数?

java - 无法使用java将byte []插入MySQL

android - 使用 convertView 时出现问题,不使用时出现问题

android - 如何在 fragment 中实现抽屉导航