繁体   English   中英

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

[英]Google Indexing API: Permission denied. Failed to verify the URL ownership

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

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

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

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

我读到的大多数 403 错误都是通过将客户端 email 添加为 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');
  }
}

您是在新的 Search Console 还是旧的 Search Console中添加了您的客户端 email ? 在新版本中,“所有者”不是我的选项(只有“完整”或“受限”),我必须将 go 到旧版本中才能添加另一个所有者。

你必须改变这条线

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM