无法使用服务帐户查询 Google Search Console API

2024-05-06

我需要使用服务帐户从 Google Search Console(网站管理员工具)检索一些数据。

到目前为止我已经能够检索到access_token对于我需要附加到请求的 url 的服务帐户。问题是我找不到办法这样做,这是我正在使用的代码:

function retrieveSearchesByQuery(token)
    {
        gapi.client.webmasters.searchanalytics.query(
        {
            'access_token': token,
            'siteUrl': 'http://www.WEBSITE.com',
            'fields': 'responseAggregationType,rows',
            'resource': {
                'startDate': formatDate(cSDate),
                'endDate': formatDate(cEDate),
                'dimensions': [
                    'date'
                ]
            }
        })
        .then(function(response) {
            console.log(response);
        })
        .then(null, function(err) {
            console.log(err);
        });
    }

这是该函数调用的 url:

https://content.googleapis.com/webmasters/v3/sites/http%3A%2F%2Fwww.WEBSITE.com/searchAnalytics/query?fields=responseAggregationType%2Crows&alt=json"

相反,它应该是这样的:

https://content.googleapis.com/webmasters/v3/sites/http%3A%2F%2Fwww.WEBSITE.com/searchAnalytics/query?fields=responseAggregationType%2Crows&alt=json&access_token=XXX"

The gapi.client.webmasters.searchanalytics.query不认识'access_token'作为有效的密钥,因此它不会将其附加到网址中,这就是为什么我得到一个401 Unauthorized作为回应。

如果我使用'key'代替'access_token'参数被附加到 url 但'key'是用来OAuth2身份验证,因此我传递的服务帐户令牌无效。

有人对此有解决方案或解决方法吗?


如果您的应用程序请求私有数据,则该请求必须由有权访问该数据的经过身份验证的用户授权。如指定文档 https://developers.google.com/webmaster-tools/v3/how-tos/authorizingSearch Console API 的一部分,您的应用程序必须使用 OAuth 2.0 来授权请求​​。不支持其他授权协议。

如果您的申请是正确配置 https://developers.google.com/api-client-library/javascript/features/authentication,当使用 Google API 时,经过身份验证的请求看起来与未经身份验证的请求完全相同。如中所述文档 https://developers.google.com/api-client-library/javascript/features/authentication,如果应用程序收到 OAuth 2.0 令牌,JavaScript 客户端库会自动将其包含在请求中。

您提到您已经检索到access_token,如果正确接收,API客户端会自动为您发送此令牌,您不必自己附加它。

一个非常基本的身份验证工作流程,一旦通过身份验证,发送请求将类似于以下代码。 Search Console API 可以使用以下范围:https://www.googleapis.com/auth/webmasters and https://www.googleapis.com/auth/webmasters.readonly.

var clientId = 'YOUR CLIENT ID';
var apiKey = 'YOUR API KEY';
var scopes = 'https://www.googleapis.com/auth/webmasters';

function auth() {
  // Set the API key.
  gapi.client.setApiKey(apiKey);

  // Start the auth process using our client ID & the required scopes.
  gapi.auth2.init({
      client_id: clientId,
      scope: scopes
  })
  .then(function () {
    // We're authenticated, let's go...
    // Load the webmasters API, then query the API
    gapi.client.load('webmasters', 'v3')
      .then(retrieveSearchesByQuery);
  });
}

// Load the API client and auth library
gapi.load('client:auth2', auth);

此时,您的retrieveSearchesByQuery函数将需要修改,因为它不再需要通过参数获取令牌才能在查询中传递它。 JavaScript 客户端库应自动将其包含在请求中。

您还可以使用API浏览器 https://developers.google.com/apis-explorer/#p/webmasters/v3/webmasters.searchanalytics.query检查特定查询支持哪些参数并检查关联的请求。

如果您需要使用外部生成的访问令牌(服务帐户应该是这种情况),则需要使用gapi.auth.setToken method https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiauthsetToken设置OAuth 2.0 令牌对象 https://developers.google.com/api-client-library/javascript/reference/referencedocs#oauth-20-token-object自己申请:

gapi.auth.setToken(token_Object);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用服务帐户查询 Google Search Console API 的相关文章

随机推荐