簡體   English   中英

在javascript / node.js中連接到Gmail IMAP API

[英]Connecting to the Gmail IMAP API in javascript/node.js

我正在嘗試通過其IMAP API連接到gmail。 我正在使用Bruno Morency的node-imap庫 為了創建oauth_signature,timestamp和nonce,我使用另一個庫

更具體一點:資源所有者已經對消費者進行了身份驗證。 所以我確實有一個訪問令牌+秘密。 當然我也有消費者的秘密+代幣。 所以我想要的是使用此處描述的XOAuth機制登錄(標題:SASL初始客戶端請求)。

執行代碼時出現錯誤:

Error while executing request: Invalid credentials d43if2188869web.36

我不知道我做錯了什么。 實際上可能有更多的原因。 錯誤的base64編碼(雖然編碼可能正常,因為你得到不同的編碼不同的錯誤,我很確定這不是它),錯誤的簽名計算(更新:我現在用http://oauth.net/core/測試了這個1.0a / #sig_base_example ),nonce計算或其他。

我可以在java應用程序中使用相同的憑據(使用者+ ressource-owner)進行身份驗證,因此憑據很可能不是錯誤的原因(只是錯誤的編碼/簽名計算)

最后的代碼。 我省略了消費者的密鑰+秘密,並沒有因為顯而易見的原因而重新提供所有者的令牌+秘密。

var oauth_version = "1.0";
var oauth_timestamp = OAuth.timestamp();
var oauth_nonce = OAuth.nonce(6); //random nonce?

var oauth_consumer_key = "NOTFORYOU"; //validated
var oauth_consumer_secret = "NOTFORYOU"; //validated
var oauth_token = "NOTFORYOU"; //validated
var oauth_token_secret = "NOTFORYOU"; //validated
var email = "NOTFORYOU"; //validated

var oauth_signature_method = "HMAC-SHA1";
var method = "GET";
var action = "https://mail.google.com/b/"
    +email
    +"/imap/"; //gmail's request url

//signature
var oauth_signature_method = "HMAC-SHA1"; //from https://developers.google.com/google-apps/gmail/oauth_protocol

//example values for validating signature from     http://oauth.net/core/1.0a/#sig_base_example
oauth_consumer_key="dpf43f3p2l4k3l03";
oauth_nonce="kllo9940pd9333jh";
oauth_signature_method="HMAC-SHA1";
oauth_timestamp="1191242096";
oauth_token="nnch734d00sl2jdk";
oauth_version="1.0";
action="http://photos.example.net/photos?file=vacation.jpg&size=original";
method="GET";

//signature
var signature_basestring_parameters = {
    oauth_version: oauth_version
    , oauth_consumer_key: oauth_consumer_key
    , oauth_timestamp: oauth_timestamp
    , oauth_nonce: oauth_nonce
    , oauth_token: oauth_token
    , oauth_signature_method: oauth_signature_method
}

//var signature_basestring = oauth_consumer_key+"&"+oauth_token_secret;
var signature_basestring = OAuth.SignatureMethod.getBaseString({method: method, action: action, parameters: signature_basestring_parameters});

var methodName = oauth_signature_method;
var signer = OAuth.SignatureMethod.newMethod(methodName, {
                    consumerSecret: oauth_consumer_secret,
                    tokenSecret: oauth_token_secret
                }
                   );
console.log("signature_basestring=["+signature_basestring+"]");

var oauth_signature = signer.getSignature(signature_basestring);

console.log("oauth_signature=["+oauth_signature+"]");

oauth_signature=OAuth.percentEncode(oauth_signature);

console.log("(escaped) oauth_signature=["+oauth_signature+"]"); //prints out tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D as in the [example](http://oauth.net/core/1.0a/#sig_base_example)

//base-string
var baseStringDecoded =  "GET"
    + " "
    + "https://mail.google.com/b/"+email+"/imap/"
    + " "
    + "oauth_consumer_key=\""+oauth_consumer_key+"\","
    + "oauth_nonce=\""+oauth_nonce+"\","
    + "oauth_signature=\""+oauth_signature+"\","
    + "oauth_signature_method=\""+oauth_signature_method+"\","
    + "oauth_timestamp=\""+oauth_timestamp+"\","
    + "oauth_token=\""+oauth_token+"\","
    + "oauth_version=\""+oauth_version+"\"";

var baseString = Base64.encode(  //base64 from http://www.webtoolkit.info/javascript-base64.html
    baseStringDecoded
);


//create imap connection
var imap = new ImapConnection({
                  host: 'imap.gmail.com',
                  port: 993,
                  secure: true,
                  debug: true,
                  xoauth: baseString
              });

更新:我找到了一個如何生成基本簽名字符串的示例 基於此,我改變了我的代碼。 因此,現在我得到相同的簽名結果(生成用於簽名的基本字符串,計算簽名值,百分比編碼簽名值),如示例中所示。 這意味着我(即使用的oauth庫)最有可能以正確的方式計算oauth_signature,而其他東西出錯了。

最后我成功了。 我最后的問題是我更改了oauth.js中的密鑰以測試oauth示例,將其更改回來完成了工作。

因此,上面的示例現在應該可以在gmail IMAP API上進行身份驗證

如果您認為它的編碼相關檢查如何在node.js中進行Base64編碼? 對於node的bas64而不是webtoolkit。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM