我想通过Google Apps脚本以G Suite管理员身份更新域用户的签名。
我使用了对Gmail API的HTTP请求,跟随迈克的示例here,并且能够使用它。
以下是我所做的总结:
在Google服务和Google API控制台中启用了Gmail API
在我的项目中添加了OAuth2 for Apps Script库(因为Apps脚本没有为OAuth2协议提供内置支持)
为我的项目创建了一个服务帐户
授予服务帐户的域范围权限
添加" https://www.googleapis.com/auth/gmail.settings.sharing"范围
以下代码适用于GREAT(请参阅原始版本here)
function setSignatureWithHTTPRequest(email, signature) {
var signatureSetSuccessfully = false;
var authorizationScope = ['https://www.googleapis.com/auth/gmail.settings.sharing'];
var service = getDomainWideDelegationService("Gmail: ", authorizationScope, email);
if (!service.hasAccess()) {
Logger.log("failed to authenticate as user " + email);
Logger.log(service.getLastError());
signatureSetSuccessfully = service.getLastError();
return signatureSetSuccessfully;
} else {
Logger.log("successfully authenticated as user " + email);
}
var username = email.split("@")[0];
var resource = { signature: signature };
var requestBody = {};
requestBody.headers = {"Authorization": "Bearer " + service.getAccessToken()};
requestBody.contentType = "application/json";
requestBody.method = "PUT";
requestBody.payload = JSON.stringify(resource);
requestBody.muteHttpExceptions = false;
var emailForUrl = encodeURIComponent(email);
var url = "https://www.googleapis.com/gmail/v1/users/me/settings/sendAs/" + emailForUrl;
try {
var setSignatureResponse = UrlFetchApp.fetch(url, requestBody);
signatureSetSuccessfully = true;
Logger.log("setSignatureResponse on successful attempt:" + setSignatureResponse);
} catch (e) {
Logger.log("Set signature with HTTP request failed: " + e);
}
return signatureSetSuccessfully;
}

但是,当我修改代码以使用Gmail API中的patch
方法而不是UrlFetchApp
的HTTP请求时,我收到GoogleJsonResponseException: Not Found
错误。
function setSignatureWithMethod(email, signature) {
var signatureSetSuccessfully = false;
var authorizationScope = ['https://www.googleapis.com/auth/gmail.settings.sharing'];
var service = getDomainWideDelegationService("Gmail: ", authorizationScope, email);
if (!service.hasAccess()) {
Logger.log("failed to authenticate as user " + email);
Logger.log(service.getLastError());
signatureSetSuccessfully = service.getLastError();
return signatureSetSuccessfully;
} else {
Logger.log("successfully authenticated as user " + email);
}
var resource = { signature: signature };
try {
var setSignatureResponse = Gmail.Users.Settings.SendAs.patch(resource, "me", email);
Logger.log("setSignatureResponse on successful attempt:" + setSignatureResponse);
signatureSetSuccessfully = true;
} catch (e) {
Logger.log("Set signature with method failed: " + e);
}
return signatureSetSuccessfully;
}

我的问题是双重的。
为什么function setSignatureWithMethod()
会产生错误?
另一张提到here的海报" ...您需要使用Gmail REST界面使用UrlFetchApp服务来拨打电话而不是Gmail服务...因为脚本不能作为服务帐户运行,仅作为用户帐户运行。"这准确吗? Gmail API提供的方法(列表,修补程序,更新等)是仅用于设置您自己的电子邮件签名(或自己的Gmail设置),而不是用于域中的其他用户吗?
答案 0 :(得分:0)
好的,有两件事。
1 - Gmail.Users.Settings.SendAs.patch()
无法工作。我认为这是Gmail API如何以任何语言实施的示例,或者是Google提供的here的一部分,无法在Apps脚本中使用
尝试更改此行:
var setSignatureResponse = Gmail.Users.Settings.SendAs.patch(resource, "me", email);
到这个区块:
var requestBody = {};
requestBody.headers = {"Authorization": "Bearer " + service.getAccessToken()};
requestBody.contentType = "application/json";
requestBody.method = "PATCH";
requestBody.payload = JSON.stringify(resource);
requestBody.muteHttpExceptions = false;
var emailForUrl = encodeURIComponent(email);
var url = "https://www.googleapis.com/gmail/v1/users/me/settings/sendAs/" + emailForUrl;
try {
var setSignatureResponse = UrlFetchApp.fetch(url, requestBody);
signatureSetSuccessfully = true;
Logger.log("setSignatureResponse on successful attempt:" + setSignatureResponse);
} catch (e) {
Logger.log("Set signature with HTTP request failed: " + e.message);
}
2 - 不确定这是否重要但您发送数组作为授权范围,但我的原始示例和所有工作代码都使用字符串。
尝试更改此内容:
var authorizationScope = ['https://www.googleapis.com/auth/gmail.settings.sharing'];
到此......
var authorizationScope = 'https://www.googleapis.com/auth/gmail.settings.sharing';