2015-06-13 53 views
3

我想從在Linux服務器上運行的NodeJS發送電子郵件到谷歌Gmail RESR HTTP API。不使用庫,只需發送https。我已經找出了OAuth部分,有一個訪問令牌,並從谷歌獲得響應。但我無法通過各種錯誤消息。我已經發布了下面的代碼。這是不明顯的,但EmailSend()後,我從谷歌獲得訪問令牌,所以是它被調用。使用通過谷歌http休息API發送電子郵件

var emailStr = new Buffer(
     "Content-Type: text/plain; charset=\"UTF-8\"\n" + 
     "MIME-Version: 1.0\n" + 
     "Content-Transfer-Encoding: 7bit\n" + 
     "to: [email protected]\n" + 
     "from: [email protected]\n" + 
     "subject: Subject Text\n\n" + 

     "The actual message text goes here" 
).toString("base64").replace(/\+/g, '-').replace(/\//g, '_'); 
//var emailBase64UrlSafe = Rtrim(emailStr, '='); 
//var emailBase64UrlSafe = JsStrToUrlSafe (emailStr); 
var emailBase64UrlSafe = emailStr; 

var http = require('https'); 
function EmailSend() { 

    var post_data = emailBase64UrlSafe; 
    var post_options = { 
     hostname: 'www.googleapis.com', 
     port: '443', 
     path: '/gmail/v1/users/me/messages/send', 
     method: 'POST', 
     headers: { 
     "Authorization": 'Bearer '+googleAccessKey['access_token'], 
     "Content-Type" : "application/json; charset=UTF-8" 
     }, 
    }; 
    console.log(post_options); 

    var post_req = http.request(post_options, function(res) { 
     res.setEncoding('utf8'); 
     res.on('data', function (chunk) { 
      console.log('Response: ' + chunk); 
     }); 
    }); 
    post_req.write(JSON.stringify({ "raw": emailBase64UrlSafe })); 
    post_req.end(); 
}; /* end EmailSend() */ 
Response: { 
"error": { 
"errors": [ 
    { 
    "domain": "global", 
    "reason": "failedPrecondition", 
    "message": "Bad Request" 
    } 
], 
"code": 400, 
"message": "Bad Request" 
} 

資源:

  1. https://tools.ietf.org/html/rfc2822#appendix-A

  2. https://developers.google.com/identity/protocols/OAuth2ServiceAccount

  3. https://nodejs.org/api/https.html#https_https_request_options_callback

  4. Send email using Google API with only access token

+0

我無法獲得這些資源的帖子,而不會弄亂格式。所以他們在這裏。 1. https://tools.ietf.org/html/rfc2822#appendix-A 2. https://developers.google.com/identity/protocols/OAuth2ServiceAccount 3. https://nodejs.org/api/ https.html#https_https_request_options_callback 4. http://stackoverflow.com/questions/29504289/send-email-using-google-api-with-only-access-token?rq=1 – user3356715

+0

您是否正在進行域名範圍授權和模擬用戶爲您的服務帳戶?檢查此鏈接https://developers.google.com/drive/web/delegation,否則它會拋出此錯誤。 – SGC

+0

查看文檔,您需要發佈一個對象,其中的消息只是其中的一部分:https://developers.google.com/gmail/api/v1/reference/users/messages/send – gcbirzan

回答

1

試了一下我自己,和它的工作!

var http = require('https'); 

var mail = new Buffer(
    "From: [email protected]\n" + 
    "To: [email protected]\n" + 
    "Subject: Subject Text\n\n" + 

    "Message text" 
).toString("base64").replace(/\+/g, '-').replace(/\//g, '_'); 


var post_options = { 
    hostname: 'www.googleapis.com', 
    port: '443', 
    path: '/gmail/v1/users/me/messages/send', 
    method: 'POST', 
    headers: { 
    "Authorization": 'Bearer <ACCESS_TOKEN>', 
    "Content-Type" : "application/json" 
    } 
}; 

var post_req = http.request(post_options, function(res) { 
    res.setEncoding('utf8'); 
    res.on('data', function (chunk) { 
     console.log('Response: ' + chunk); 
    }); 
}); 

post_req.write(JSON.stringify({ "raw": mail })); 
post_req.end(); 
相關問題