【问题标题】:Google Blogger API error 400Google Blogger API 错误 400
【发布时间】:2017-03-27 15:01:20
【问题描述】:

我正在尝试通过 JavaScript V3 API 访问 Blogger。如果我尝试访问我的(公共)测试博客,一切都会按预期进行。

如果我使用相同的代码但尝试访问我的(私人)测试博客,我会收到错误消息。

以下是我的代码

        var client_id = ['WWWW', 'XXXX'];
        var api_key = ['YYYY','ZZZZ'];
        var discoveryDocs = ['https://www.googleapis.com/discovery/v1/apis/blogger/v3/rest'];
        var scope = 'https://www.googleapis.com/auth/blogger.readonly';
        var blog_id = ['BLOG0', 'BLOG1'];

        var appendResults = function(results) {
          $('#results').append(JSON.stringify(results, undefined, 2) + '<hr/>');
        };

        getBlogs = function(client, key, blog) {
            gapi.client.init({
                'apiKey': key,
                'clientId': client,
                'discoveryDocs': discoveryDocs,
                'scope': scope
            }).then(function() {
                return gapi.client.blogger.posts.list({
                    'blogId': blog
                });
            }).then(function(d) {
                return d;
            }).then(function(response) {
                appendResults(response);
            }, function(reason) {
                appendResults(reason);
            });
        };

        gapi.load('client', function() {
            for(i=0; i<api_key.length; i++) {
                getBlogs(client_id[i], api_key[i], blog_id[i]);
            }
        });

其中 api_key 和 blog_id 中的第二个元素是我的私人博客。

以下是我的回复

{
  "result": {
    "kind": "blogger#postList",
    "items": [
      {
        "kind": "blogger#post",
        "id": "XXXX",
        "blog": {
          "id": "XXXX"
        },
        "published": "XXXX",
        "updated": "XXXX",
        "etag": "\"XXXX\"",
        "url": "http://XXXX/2017/03/blog-post.html",
        "selfLink": "https://www.googleapis.com/blogger/v3/blogs/XXXX/posts/XXXX",
        "title": "",
        "content": "XXXX",
        "author": {
          "id": "XXXX",
          "displayName": "XXXX",
          "url": "https://www.blogger.com/profile/XXXX",
          "image": {
            "url": "XXXX"
          }
        },
        "replies": {
          "totalItems": "0",
          "selfLink": "XXXX"
        }
      }
    ],
    "etag": "\"XXXX\""
  },
  "body": "{\n \"kind\": \"blogger#postList\",\n \"items\": [\n  {\n   \"kind\": \"blogger#post\",\n   \"id\": \"XXXX\",\n   \"blog\": {\n    \"id\": \"639440130428294175\"\n   },\n   \"published\": \"XXXX\",\n   \"updated\": \"XXXX\",\n   \"etag\": \"\\\"XXXX\\\"\",\n   \"url\": \"http://XXXX/2017/03/blog-post.html\",\n   \"selfLink\": \"https://www.googleapis.com/blogger/v3/blogs/XXXX/posts/XXXX\",\n   \"title\": \"\",\n   \"content\": \"XXXX\",\n   \"author\": {\n    \"id\": \"XXXX\",\n    \"displayName\": \"XXXX\",\n    \"url\": \"https://www.blogger.com/profile/XXXX\",\n    \"image\": {\n     \"url\": \"XXXX\"\n    }\n   },\n   \"replies\": {\n    \"totalItems\": \"0\",\n    \"selfLink\": \"https://www.googleapis.com/blogger/v3/blogs/XXXX/posts/XXXX/comments\"\n   }\n  }\n ],\n \"etag\": \"\\\"XXXX\\\"\"\n}\n",
  "headers": {
    "date": "XXXX",
    "content-encoding": "gzip",
    "vary": "Origin, X-Origin",
    "content-length": "576",
    "pragma": "no-cache",
    "server": "GSE",
    "etag": "\"XXXX\"",
    "content-type": "application/json; charset=UTF-8",
    "cache-control": "no-cache, no-store, max-age=0, must-revalidate",
    "expires": "XXXX"
  },
  "status": 200,
  "statusText": null
}

{
  "result": {
    "error": {
      "errors": [
        {
          "domain": "global",
          "reason": "invalid",
          "message": "We're sorry, but the requested resource could not be found."
        }
      ],
      "code": 400,
      "message": "We're sorry, but the requested resource could not be found."
    }
  },
  "body": "{\n \"error\": {\n  \"errors\": [\n   {\n    \"domain\": \"global\",\n    \"reason\": \"invalid\",\n    \"message\": \"We're sorry, but the requested resource could not be found.\"\n   }\n  ],\n  \"code\": 400,\n  \"message\": \"We're sorry, but the requested resource could not be found.\"\n }\n}\n",
  "headers": {
    "date": "XXXX",
    "content-encoding": "gzip",
    "vary": "Origin, X-Origin",
    "content-length": "160",
    "server": "GSE",
    "content-type": "application/json; charset=UTF-8",
    "cache-control": "private, max-age=0",
    "expires": "XXXX"
  },
  "status": 400,
  "statusText": null
}

我已经在开发者控制台中设置了凭据。我正在通过 localhost:5000/ 访问它,我已将 localhost 设置为开发人员控制台中的有效域。

我能想到的唯一线索是

"result": {
    "error": {
      "errors": [
        {
          "domain": "global",
          "reason": "invalid",
          "message": "We're sorry, but the requested resource could not be found."
        }
      ],
      "code": 400,
      "message": "We're sorry, but the requested resource could not be found."
    }

这可能告诉我我的请求来源无效,但我不能确定。

如果我将同一个(私人)博客公开并重新执行,则此代码适用于两个博客 ID。

我哪里出错了?

【问题讨论】:

  • 您需要 OAuth 授权才能访问私人博客。请参阅developers.google.com/blogger/docs/3.0/reference/posts/list(如果帖子在私人博客上,则需要授权)。此外,范围需要从 https://www.googleapis.com/auth/blogger.readonly 更改为 https://www.googleapis.com/auth/blogger

标签: javascript api blogger


【解决方案1】:

Blogger API 要求您的应用程序在访问私人博客上的信息时具有 OAuth 2.0 令牌(请参阅 documentation here)。仅使用 API 密钥不会使请求生效。

如果帖子位于私人博客上,则需要Authorization。如果帖子在公开的博客上,则可以在未经授权的情况下调用此方法。

此外,使用 OAuth 令牌时范围不同 -

以下是 Blogger API 的 OAuth 2.0 范围信息:

https://www.googleapis.com/auth/blogger 

要使用 OAuth 2.0 请求访问权限,您的应用程序需要范围信息以及 Google 在应用程序注册期间提供的信息(例如 作为客户端 ID 和/或客户端密码)。

你可以通过https://developers.google.com/apis-explorer/#search/blogger.posts.list/m/blogger/v3/blogger.posts.list测试这个API

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2017-08-10
    • 1970-01-01
    相关资源
    最近更新 更多