【发布时间】:2020-09-16 13:24:12
【问题描述】:
我在 angular9 中创建了一次服务并调用 google api 发送电子邮件,但它不起作用。有人可以帮助我该怎么办吗?我尝试了许多其他方法,但没有一种方法奏效。
sendEmail() {
const scopes = [
'https://www.googleapis.com/auth/gmail.send',
].join(' ');
const message =
"From: sample@gmail.com\r\n" +
"To: sample2@gmail.com\r\n" +
"Subject: As basic as it gets\r\n\r\n" +
"This is the plain text body of the message. Note the blank line between the header information and the body of the message.";
const encodedMessage = btoa(message)
const reallyEncodedMessage = encodedMessage.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
gapi.load('client:auth2', () => {
gapi.client.load('gmail', 'v1', () => {
console.log('Loaded Gmail');
gapi.client.init({
apiKey: this.apiKey,
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest'],
client_id: this.clientId,
immediate: true,
scope: scopes
}).then(res => {
console.log('pop')
return gapi.client.gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: reallyEncodedMessage
}
}).then(res => {
console.log("done!", res)
});
})
});
});
}
gapi 的回应:
kind: "discovery#restDescription"
id: "gmail:v1"
name: "gmail"
version: "v1"
rootUrl: "https://www.googleapis.com/"
servicePath: "gmail/v1/users/"
batchPath: "batch/gmail/v1"
...
【问题讨论】:
标签: javascript angular gmail-api angular9 google-api-js-client