在 GMail 中添加 sendAsMail 别名
Adding sendAsMail Alias in GMail
我一直在尝试从有时 visa 应用程序脚本中添加另一个 sendasmail 别名,但端点似乎不起作用,或者我不确定这是否是它的预期行为。
这是我正在使用的端点 https://gmail.googleapis.com/gmail/v1/users/%s/settings/sendAs/%s',getEmail, "007@alias.domain.com"
。
这是 AppScipt 代码以及我正在尝试使用的负载。
var service = getOAuthService(getEmail);
var sendAPIUrl = Utilities.formatString('https://gmail.googleapis.com/gmail/v1/users/%s/settings/sendAs/%s',getEmail, "007@alias.domain.com");
console.log(sendAPIUrl);
var response = UrlFetchApp.fetch(sendAPIUrl, {
method: "PUT",
muteHttpExceptions: true,
contentType: "application/json",
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
payload: JSON.stringify({
"sendAsEmail": "007@alias.domain.com",
"isDefault": true,
"replyToAddress": "007@alias.domain.com",
"treatAsAlias": true
})
});
在 getEmail 中,它包含用户的主要电子邮件地址,而 getOauthService 基本上是用户模拟的另一个功能,如更改 senAsEmail;只有服务帐户可以工作。
我尝试发送 POST 请求来创建一个,但失败并显示错误代码,而域别名列表中允许别名模式并且别名可用。
这是 POST
的代码
var service = getOAuthService(getEmail);
var sendAPIUrl =
Utilities.formatString('https://gmail.googleapis.com/gmail/v1/users/%s/settings/sendAs',getEmail);
console.log(sendAPIUrl);
var response = UrlFetchApp.fetch(sendAPIUrl, {
method: "POST",
muteHttpExceptions: true,
contentType: "application/json",
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
payload: JSON.stringify({
"sendAsEmail": "007@alias.domain.com",
})
});
console.log(response.getContentText())
这是我得到的错误。
{
"error": {
"code": 400,
"message": "sendAsEmail is not a valid user or group",
"errors": [
{
"message": "sendAsEmail is not a valid user or group",
"domain": "global",
"reason": "invalidArgument"
}
],
"status": "INVALID_ARGUMENT"
}
}
您需要创建别名而不是更新它
- 当您向 https://gmail.googleapis.com/gmail/v1/users/{userId}/settings/sendAs/{sendAsEmail} endpoint, you can update 一个已经存在的别名发出
PUT
请求时
- 因此,需要在请求体中指定新的
{ "sendAsEmail": "" }
,在请求URL 中指定旧的sendAs
- 如果您想要 create a new, non-existing alias, you need to make a
POST
request to the https://gmail.googleapis.com/gmail/v1/users/{userId}/settings/sendAs 端点
另见 here。
我一直在尝试从有时 visa 应用程序脚本中添加另一个 sendasmail 别名,但端点似乎不起作用,或者我不确定这是否是它的预期行为。
这是我正在使用的端点 https://gmail.googleapis.com/gmail/v1/users/%s/settings/sendAs/%s',getEmail, "007@alias.domain.com"
。
这是 AppScipt 代码以及我正在尝试使用的负载。
var service = getOAuthService(getEmail);
var sendAPIUrl = Utilities.formatString('https://gmail.googleapis.com/gmail/v1/users/%s/settings/sendAs/%s',getEmail, "007@alias.domain.com");
console.log(sendAPIUrl);
var response = UrlFetchApp.fetch(sendAPIUrl, {
method: "PUT",
muteHttpExceptions: true,
contentType: "application/json",
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
payload: JSON.stringify({
"sendAsEmail": "007@alias.domain.com",
"isDefault": true,
"replyToAddress": "007@alias.domain.com",
"treatAsAlias": true
})
});
在 getEmail 中,它包含用户的主要电子邮件地址,而 getOauthService 基本上是用户模拟的另一个功能,如更改 senAsEmail;只有服务帐户可以工作。
我尝试发送 POST 请求来创建一个,但失败并显示错误代码,而域别名列表中允许别名模式并且别名可用。
这是 POST
的代码 var service = getOAuthService(getEmail);
var sendAPIUrl =
Utilities.formatString('https://gmail.googleapis.com/gmail/v1/users/%s/settings/sendAs',getEmail);
console.log(sendAPIUrl);
var response = UrlFetchApp.fetch(sendAPIUrl, {
method: "POST",
muteHttpExceptions: true,
contentType: "application/json",
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
payload: JSON.stringify({
"sendAsEmail": "007@alias.domain.com",
})
});
console.log(response.getContentText())
这是我得到的错误。
{
"error": {
"code": 400,
"message": "sendAsEmail is not a valid user or group",
"errors": [
{
"message": "sendAsEmail is not a valid user or group",
"domain": "global",
"reason": "invalidArgument"
}
],
"status": "INVALID_ARGUMENT"
}
}
您需要创建别名而不是更新它
- 当您向 https://gmail.googleapis.com/gmail/v1/users/{userId}/settings/sendAs/{sendAsEmail} endpoint, you can update 一个已经存在的别名发出
PUT
请求时 - 因此,需要在请求体中指定新的
{ "sendAsEmail": "" }
,在请求URL 中指定旧的 - 如果您想要 create a new, non-existing alias, you need to make a
POST
request to the https://gmail.googleapis.com/gmail/v1/users/{userId}/settings/sendAs 端点
sendAs
另见 here。