Google 索引 API:权限被拒绝。无法验证 URL 所有权

And*_*ton 1 google-api google-apps-script google-oauth google-indexing-api

我已经浏览了所有其他线程,不幸的是他们仍然没有解决我的问题,所以我希望你能帮忙!我正在尝试通过 Google App Script 使用 Google 的索引 API。到目前为止,我已经:

循序渐进地遵循文档;创建了一个服务帐户,将我的应用程序脚本连接到我的 GC 项目,并将我的客户电子邮件添加为搜索控制台属性的所有者

我还包含了我的应用程序脚本清单文件以包含以下 oauthScopes:

    "oauthScopes": [
    "https://www.googleapis.com/auth/indexing",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/userinfo.email"
  ]
Run Code Online (Sandbox Code Playgroud)

我读过的大多数 403 错误都是通过将客户端电子邮件添加为 Search Console 属性的所有者来解决的,但就我而言,这并没有解决问题。如果需要,很乐意提供更多详细信息!:)

这是我的 Google App 脚本:

var Json = {
    "private_key": "###",
    "client_email": "###",
    "client_id": "###",
    "user_email": "###"
};


/**
 * Authorizes and makes a request to the Google+ API.
 */
function run() {

  var requestBody = {
       "url":"http://testymctestface.com",
       "type":"URL_UPDATED"
}

   var options = {
        'method': 'POST',
        'contentType': 'application/json',
        'headers': {
            'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
        },
        'payload': JSON.stringify(requestBody),
        'muteHttpExceptions': true
    };

        var service = getService();
        var url = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
        var response = UrlFetchApp.fetch(url,options);
        Logger.log(response);

}

/**
 * Reset the authorization state, so that it can be re-tested.
 */
function reset() {

  var service = getService();
  service.reset();
}

/**
 * Configures the service.
 */
function getService() {
  return OAuth2.createService('Indexing API')
      // Set the endpoint URLs.
      .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')

      // Set the client ID and secret.
      .setPrivateKey(Json.private_key)
      .setIssuer(Json.client_email)
      .setSubject(Json.user_email)
      .setClientId(Json.client_id)

      // Set the name of the callback function that should be invoked to complete
      // the OAuth flow.
      .setCallbackFunction('authCallback')

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties())

      // Set the scope and additional Google-specific parameters.
      .setScope('https://www.googleapis.com/auth/indexing')
      .setParam('access_type', 'offline')
      .setParam('approval_prompt', 'force')
      .setParam('login_hint', Session.getActiveUser().getEmail());
}

/**
 * Handles the OAuth callback.
 */
function authCallback(request) {
  var service = getService();
  var authorized = service.handleCallback(request);
  if (authorized) {
    return HtmlService.createHtmlOutput('Success!');
  } else {
    return HtmlService.createHtmlOutput('Denied');
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

您是在新 Search Console 还是旧 Search Console 中添加客户电子邮件?“所有者”不是我在新版本中的选项(只有“完整”或“受限”),我不得不去旧版本添加另一个所有者。