我已经仔细研究了所有其他线程,很遗憾,他们仍然没有解决我的问题,因此希望您能提供帮助!我正在尝试通过Google App脚本使用Google的Indexing API。到目前为止,我已经:
按照文档逐步进行;创建了一个服务帐户,将我的应用程序脚本连接到我的GC项目,并添加了我的客户电子邮件作为Search Console属性的所有者。
我还包括了我的应用脚本清单文件,其中包括以下oauthScopes:
"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属性的所有者来解决,但就我而言,这还不能解决问题。如有需要,乐意提供更多详细信息! :)
这是我的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');
}
}
答案 0 :(得分:1)
您将客户电子邮件添加到新的Search Console中还是old Search Console?在新的所有者中,“所有者”不是我的选择(只能是“完整”或“受限制”),我不得不转到旧的所有者中添加另一个所有者。
答案 1 :(得分:0)
您必须更改此行
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
在
'Authorization': 'Bearer '+ service.getAccessToken()