Gmail XOAUTH 2.0,IMAP AUTHENTICATE返回“状态”:“400” [英] Gmail XOAUTH 2.0, IMAP AUTHENTICATE returns "status":"400"

查看:1367
本文介绍了Gmail XOAUTH 2.0,IMAP AUTHENTICATE返回“状态”:“400”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用XOAUTH2来验证IMAP对Gmail的呼叫(是的,我知道有一个Gmail API,但我有理由坚持使用IMAP)。



我使用以下方式获取有效标记:

  GoogleAuthUtil.getToken(上下文环境,字符串帐户,oauth2: + GmailScopes.MAIL_GOOGLE_COM)



<$ p $ b $ GoogleAccountCredential凭证= GoogleAccountCredential.usingOAuth2(
上下文,Arrays.asList(SCOPES))
.setBackOff(新的ExponentialBackOff())
.setSelectedAccountName(account) ;
credential.getToken();

它们都返回相同的标记,因此无论哪种方式似乎都行得通。
返回的标记根据此有效:


https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=xyz




返回:

  {
issued_to:myclientid。 ... apps.googleusercontent.com,
audience:myclientid .... apps.googleusercontent.com,
范围:https://mail.google.com https ://bmail.google.com/,
expires_in:619,
access_type:online
}

然而,如果我在我的imap会话中调用AUTHENTICATE,像这样:

  AUTHENTICATE XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQFhdXRoPUJlYXJlciB2RjlkZnQ0cW1UYzJOdmIzUmxja0JoZEhSaGRtbHpkR0V1WTI5dENnPT0BAQo = 

与base64编码部分是:


user=emanuel.moecklin@gmail.com^Aauth=持票人ya29.Aw (^ A代表控制A,如0x1)


$ p $($ A $表示控制A在0x1中)

我仍然收到回应:
{status:400,schemes:承载者,范围: https://mail.google.com/ }



这似乎表明某事是错误的

我使用了正确的范围,该令牌的客户端ID与我在Google开发者控制台中的应用的客户端ID相匹配,指纹具有正确的值,包名称与我应用的包名称相匹配,所以我做错了什么?

解决方案

是一个初学者的错误。
IMAP命令参数是这样构建的:

  String para =user =+ myuser + 0x1 + auth = Bearer+令牌+ 0x1 + 0x1; 

不幸的是,0x1并不转化为^ A(ascii code 1),而是转换为1(number 1) 。



我需要做的是:

  String para = user =+ myuser +(char)0x1 +auth = Bearer+ token +(char)0x1 +(char)0x1; 

我留下了问题,因为它包含了一些关于如何使用IMAP实现XOAUTH2的好信息,实施XOAUTH2知道这是多么令人困惑(考虑多少/少/过时/矛盾的文件可以找到一个主题)。

I'm trying to use XOAUTH2 to authenticate IMAP calls to Gmail (yes I'm aware there is a Gmail API but I have my reasons to stick to IMAP).

I get a valid token by either using:

GoogleAuthUtil.getToken(Context context, String account, "oauth2:" + GmailScopes.MAIL_GOOGLE_COM)

or

GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
                    context, Arrays.asList(SCOPES))
                    .setBackOff(new ExponentialBackOff())
                    .setSelectedAccountName(account);
credential.getToken();

They both return the same token so either way seems to work. The returned tokens are valid according to this:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=xyz

Returns:

{
 "issued_to": "myclientid....apps.googleusercontent.com",
 "audience": "myclientid....apps.googleusercontent.com",
 "scope": "https://mail.google.com https://mail.google.com/",
 "expires_in": 619,
 "access_type": "online"
}

Nevertheless if I do call AUTHENTICATE in my imap session like so:

AUTHENTICATE XOAUTH2 dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQFhdXRoPUJlYXJlciB2RjlkZnQ0cW1UYzJOdmIzUmxja0JoZEhSaGRtbHpkR0V1WTI5dENnPT0BAQo=

with the base64 encoded part being:

user=emanuel.moecklin@gmail.com^Aauth=Bearer ya29.AwKAJ0L6Wm06wxMd4rhIl0YHrsnnWyIJ9XPSlCRhhx2XffuP5F8ibptTOMjGP8WELkUCYQ^A^A

(^A stands for Control A as in 0x1)

I still get the response: {"status":"400","schemes":"Bearer","scope":"https://mail.google.com/"}

which seems to indicate that "something" is wrong with the token (expired, missing scope or whatnot).

I used the correct scope, the client id of the token matches my apps' client id in the Google developer console, the fingerprint has the correct value and the package name matches my apps' package name so what am I doing wrong?

解决方案

Nevermind it was a beginner's mistake. The IMAP command parameter was built like this:

String para = "user=" + myuser + 0x1 + "auth=Bearer " + token + 0x1 + 0x1;

Unfortunately 0x1 doesn't translate into ^A (ascii code 1) but into 1 (number 1).

What I need to do is:

String para = "user=" + myuser + (char)0x1 + "auth=Bearer " + token + (char)0x1 + (char)0x1;

I leave the question since it contains some good information on how to implement XOAUTH2 with IMAP and everyone who has implemented XOAUTH2 knows how confusing that can be (considering how much/little/outdated/contradicting documentation one can find one the topic).

这篇关于Gmail XOAUTH 2.0,IMAP AUTHENTICATE返回“状态”:“400”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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