Java Blogger JSON API添加帖子

Java Blogger JSON API添加帖子,java,android,api,request,blogger,Java,Android,Api,Request,Blogger,我想使用Blogger API向我的博客添加帖子。我成功地获得了使用BloggerAPI的权限,并在GoogleAPI控制台中激活了它们。我使用教程获取访问令牌。我找到了,所以在请求之前我获得了新的请求令牌 当我第一次请求添加帖子时,我收到一个错误:401“消息”:“无效凭据”,“位置”:“授权” 当我第二次请求使用新令牌添加帖子时,我收到错误:403“消息”:“超出每日限额。请注册” 我请求的代码是: final JSONObject obj = new JSONObject(); obj.p

我想使用Blogger API向我的博客添加帖子。我成功地获得了使用BloggerAPI的权限,并在GoogleAPI控制台中激活了它们。我使用教程获取访问令牌。我找到了,所以在请求之前我获得了新的请求令牌

当我第一次请求添加帖子时,我收到一个错误: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();
更新
找到了解决方案:只需将“?key={MY_API_key}”添加到请求的URL中,就解决了这个问题

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

在我看来,您似乎丢失了API密钥、使用错误、在代码中错误放置了它或以错误的方式将其提供给服务


我还没有看过这里的代码,但这是谷歌的示例代码,您正在尝试做什么。使用

测试您的API密钥谢谢您的回答,我尝试添加了”?key=“to request's url and it works=)真棒,很高兴为您服务:)您的问题真的帮了我很大的忙,衷心感谢您的提问和回答
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
}