gpt4 book ai didi

java - Blogger JSON API 添加帖子

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:45 24 4
gpt4 key购买 nike

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

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

当我第二次请求使用新 token 添加帖子时,出现错误: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}”即可解决问题

最佳答案

您链接的教程网站声明

"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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com