你好我想在gmail设置上添加发送参数。对于我的所有用户,并将其更改为使默认帐户发送邮件。 我知道GUI很热,但我喜欢用GAS做。 知识的代码是对特定用户的测试。
var JSON = {
"private_key": "-----BEGIN PRIVATE KEY-----\\n-----END PRIVATE KEY-----\n",
"client_email": "client..@project-id-XXXXX.gserviceaccount.com",
"client_id": "12345800",
"user_email": "mainaccount@dominio.com"
};
function getOAuthService(user) {
return OAuth2.createService('Service Account')
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
.setPrivateKey(JSON.private_key)
.setIssuer(JSON.client_email)
.setSubject(JSON.user_email)
.setPropertyStore(PropertiesService.getScriptProperties())
.setParam('access_type', 'offline')
.setParam('approval_prompt', 'force')
.setScope('https://www.googleapis.com/auth/gmail.settings.sharing')
.setScope('https://www.googleapis.com/auth/gmail.settings.basic');
}
function createAlias() {
var userEmail = 'mainaccount@dominio.com';
var alias = {
alias: 'makealiasdefault@dominio.com'
};
alias = AdminDirectory.Users.Aliases.insert(alias, userEmail);
Logger.log('Created alias %s for user %s.', alias.alias, userEmail);
var service = getOAuthService();
service.reset();
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/gmail/v1/users/'+userEmail+'/settings/sendAs'
var headers ={
"Authorization": 'Bearer ' + service.getAccessToken()
};
var options = {
'method':'post',
'headers': headers,
'method': 'GET',
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, options);
}
Logger.log(response.getContentText());
var resource ={'sendAs':[
{'isDefault':true,
'verificationStatus':'accepted',
'sendAsEmail':alias.alias,
'treatAsAlias':true}]};
Logger.log(resource);
var sendas = Gmail.Users.Settings.SendAs.create(resource,alias.alias);
Logger.log(sendas);
}
function reset() {
var service = getOAuthService();
service.reset();
}
我得到一个错误,对于mainaccount@dominio.com拒绝了代表团
如果我chagen这行var sendas = Gmail.Users.Settings.SendAs.create(resource,alias.alias); 为了这 var sendas = Gmail.Users.Settings.SendAs.create(resource,userEmail); 我得到了一个不同的错误
访问仅限于已委派域范围权限的服务帐户
制作了整个域名有些人知道如何使用此过程创建发送?或者如果代码有问题!
答案 0 :(得分:2)
以下是从您的来源清理的工作示例。您的原始代码存在一些问题:
var service_account = {
"private_key": "-----BEGIN PRIVATE KEY...",
"client_email": "sa-email@example.com",
"client_id": "1234569343",
"user_email": "useraccount@example.com"
};
function getOAuthService(user) {
return OAuth2.createService('Service Account')
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
.setPrivateKey(service_account.private_key)
.setIssuer(service_account.client_email)
.setSubject(service_account.user_email)
.setPropertyStore(PropertiesService.getScriptProperties())
.setScope('https://www.googleapis.com/auth/gmail.settings.sharing https://www.googleapis.com/auth/gmail.settings.basic')
}
function createAlias() {
var userEmail = 'useraccount@example.com';
var alias = 'myalias@example.com';
var alias_name = 'Alias User';
var service = getOAuthService();
service.reset();
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/gmail/v1/users/me/settings/sendAs'
var headers ={
"Authorization": 'Bearer ' + service.getAccessToken(),
"Accept":"application/json",
"Content-Type":"application/json",
};
var resource ={
'sendAsEmail': alias,
'displayName': alias_name
};
var options = {
'headers': headers,
'method': 'POST',
'payload': JSON.stringify(resource),
'muteHttpExceptions': true
};
Logger.log(options);
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
}
function reset() {
var service = getOAuthService();
service.reset();
}
'''