我想使用 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/