使用Gmail API在Chrome应用中发送邮件

问题描述 投票:0回答:1

我正在尝试创建Chrome应用,但我想发送电子邮件。我找到了this,但我无法弄清楚它是如何起作用的。

function sendMessage(userId, to, subject, email) {
    authUser(function() {
        var base64EncodedEmail = btoa(email);
        var request = gapi.client.gmail.users.messages.send({
            'userId': userId,
            'message': {
                'raw': base64EncodedEmail,
                'headers': [
                    {'To': to}, 
                    {'Subject': subject}
                ]
            }
        });
        request.execute();
    });
}

function authUser(callback) {
    chrome.identity.getAuthToken({'interactive': true}, function(token) {
        // load Google's javascript client libraries
        var url = "https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=" + token;
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
            if (request.readyState !== 4 || request.status !== 200) {
                return;
            }
            var response = JSON.parse(request.responseText);
            console.log(response);
            callback();
        }
        ;
        request.open('POST', url, true);
        request.send();
        request.setRequestHeader('Authorization', 'Bearer ' + token);
    });
}

我把代码放在我的background.js中(在启动时调用sendMessage),并得到以下manifest.json:

{
  "name": "Name",
  "description": "Description",
  "permissions": [
        "storage",
        { "fileSystem": ["write"] },
        "identity",
        "https://mail.google.com/"],

  "version": "1.0",
  "manifest_version": 2,
  "oauth2": {
        "client_id": "XXXXX.apps.googleusercontent.com",
        "scopes": [
            "https://www.googleapis.com/auth/userinfo.email",
                        "https://www.googleapis.com/auth/gmail.send"
    ]   
    },
    "app": {
    "background": {
      "scripts": [
            "background.js"]
    }
  },
  "icons": { "16": "images/mailling-16.png", "128": "images/mailling-128.png" }
}

因此,我了解我们需要与Gmail API建立连接(使用我们的凭据),然后我们发送邮件。但我在POST请求和服务器抛出HTTP错误400:'raw'RFC822有效负载消息字符串或通过/ upload / * URL上传消息

我不明白为什么他期待一个消息,而我们正在建立联系:我

javascript gmail-api google-chrome-app
1个回答
0
投票

我最好的解决方案是切换到google script apps,您只能使用MailApp.sendEmail发送邮件。

无论如何,这不是Chrome应用程序的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.