在javascript / node.js中连接到Gmail IMAP API [英] Connecting to the Gmail IMAP API in javascript/node.js

查看:113
本文介绍了在javascript / node.js中连接到Gmail IMAP API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过其 IMAP API 连接到Gmail。我为此使用Bruno Morency的 node-imap库。为了创建oauth_signature,时间戳和随机数,我使用另一个库

I am trying to connect to gmail via its IMAP API. I am using Bruno Morency's node-imap library for that. For creating the oauth_signature, timestamp and nonce I use another library.

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

To be more specific: The ressource-owner has already authenticated the consumer. So I do have an access-token + secret. Of course I also have the consumer's secret+token. So what I want is to login with the XOAuth mechanism described here (heading: SASL Initial Client Request).

执行代码时,出现错误:

When executing the code I get an error:

Error while executing request: Invalid credentials d43if2188869web.36

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

I wonder what I am doing wrong. Actually there might be more reasons. Wrong base64 encoding (although encoding probably works right since you get a different error for different encoding, I am pretty sure this is not it), wrong signature calculating (UPDATE: I tested this now with http://oauth.net/core/1.0a/#sig_base_example), nonce calculating or others.

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

I can authenticate using the same credentials (consumer+ressource-owner) in a java app, so credentials are most probably not the cause of the error (just wrong encoding/signature calculating)

最后是代码。我忽略了消费者的密钥+秘密,因为显而易见的原因,它们既不会重新分配所有者的令牌+秘密。)

Finally the code. I omitted consumer's key+secret neither ressource owner's token+secret for obvious reasons).

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,而其他的则会出错。

UPDATED: I found an example how to generate the base signature string. based on this I changed my code. Accordingly, now I get the same results for signature (generating base string for signature, calculating signature value, percent encoding signature value) as in the example. This means I am (i.e. the oauth library used) most probably calculating the oauth_signature in a right way and something else is going wrong.

推荐答案

最后我成功了。最后我的问题是,我改变了oauth.js中的密钥来测试oauth示例,并将其更改回来。

Finally I succeeded. My problem in the end was that I changed the key in oauth.js for testing the oauth example, changing it back did the job.

因此,上面的例子现在应该在gmail IMAP API上进行身份验证

So the the example above should now work to authenticate on the gmail IMAP API

这篇关于在javascript / node.js中连接到Gmail IMAP API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆