1

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

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

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

    "oauthScopes": [
    "https://www.googleapis.com/auth/indexing",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/userinfo.email"
  ]

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

这是我的谷歌应用脚​​本:

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');
  }
}
4

2 回答 2

8

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

于 2020-07-11T12:54:22.983 回答
-1

你必须改变这条线

'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()

'Authorization': 'Bearer '+ service.getAccessToken()

于 2020-12-04T17:32:34.393 回答