我需要获取所有带有标签的消息以进行统计。我有一个提示,因为当我执行电子邮件时,我没有任何问题,但是目标是获取gmail共享帐户的统计信息。例如,如果我使用了下一个脚本:
/**
* Lists the labels in the user's account.
*/
function listLabels() {
var response = Gmail.Users.Labels.list('test@company.com');
if (response.labels.length == 0) {
Logger.log('No labels found.');
} else {
Logger.log('Labels:');
for (var i = 0; i < response.labels.length; i++) {
var label = response.labels[i];
Logger.log('- %s', label.name);
}
}
}
test@company.com是我的共享帐户,但我总是被拒绝。我不确定共享公司或记录方法是否有问题。事实上,如果我使用共享电子邮件进行日志记录,实际上我会看到我的个人帐户。
感谢您的帮助。
使用新代码更新(JSON数据是伪造的):
var JSON = {
"private_key": "xxxx",
"client_email": "mail_shared@test.com",
"client_id": "client_id.apps.googleusercontent.com",
"user_email": "personal@test.com"
};
function getOAuthService(user) {
return OAuth2.createService("Service Account")
.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')
.setScope('https://www.googleapis.com/auth/gmail.labels');
}
function reset() {
var service = getOAuthService();
service.reset();
}
/**
* Lists the labels in the user's account.
*/
function listLabels() {
var service = getOAuthService();
service.reset();
var response = Gmail.Users.Messages.list('mail_shared@test.com');
if (response.labels.length == 0) {
Logger.log('No labels found.');
} else {
Logger.log('Labels:');
for (var i = 0; i < response.labels.length; i++) {
var label = response.labels[i];
Logger.log('- %s', label.name);
}
}
}
我尝试了下一个不同的范围:
https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/gmail.settings.basic https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/gmail.labels