有没有办法在使用 Oauth 进行 Gmail 身份验证后获取您的电子邮件地址?
在 Python Web 应用程序中,我可以使用 OAuth 和 IMAP 连接到 Gmail。 OAuth 只需使用您当前登录的任何 Google 帐户,并要求您授予对网络应用程序的访问权限。但是,它不提供 API 来实际检索该帐户的电子邮件地址。
问题是,即使用户通过 OAuth 授予访问权限,要与 Gmail 交互,您仍然需要在 IMAP 连接 URL 中显式提供电子邮件地址:
https://mail.google.com/mail/b/[your-email]/imap/
因此,Web 应用程序必须向用户询问其电子邮件地址< em>并要求他们通过 OAuth 授予 Gmail 访问权限。更糟糕的是,他们输入的电子邮件地址可能与他们授予访问权限的帐户的电子邮件地址不匹配,从而导致应用程序失败。
有没有办法使用 OAuth 获取您的电子邮件地址,以便您可以将其放入该 URL 中而无需询问用户?我尝试查看这个答案,但 Google每当我同时使用 https://mail.google.com/
和 https://www.googleapis.com/auth/userinfo# 时,都会返回
作为我的范围。错误请求
email
In a Python web application, I am able to connect to Gmail using OAuth and IMAP. OAuth uses whichever Google account you're currently signed into simply and asks you to grant access to the web app. However, it doesn't provide an API to actually retrieve that account's email address.
The problem is, even though the user grants access with OAuth, to interface with Gmail, you still need to explicitly provide the email address in your IMAP connection URL:
https://mail.google.com/mail/b/[your-email]/imap/
Because of this, the web application has to ask the user for their email address and ask them to grant access from Gmail with OAuth. What's worse is the email address they enter may not match the email of the account they grant access with, causing the app to fail.
Is there a way to get your email address using OAuth so you can put it in that URL without asking the user for it? I tried looking at this answer but Google returns a Bad Request
whenever I use both https://mail.google.com/
and https://www.googleapis.com/auth/userinfo#email
as my scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的是 Google Contacts API。如果您通过 OAuth 授权,您当前可能正在请求访问 gmail 范围的权限:
您还需要请求联系人范围的权限:
获得权限后,您可以发出类似于以下内容的 GET 请求:
这应该返回一堆 xml,以下是一些相关位:
正如您所看到的,您可以在几个地方找到授权用户的电子邮件。
如果您正在使用 OAuth,您可能还想看看 Google OAuth Playground,我发现它非常方便:http://googlecodesamples.com/oauth_playground/index.php。如果您决定使用 OAuth2,https://code.google.com/oauthplayground 上有等效工具
What you're after is the Google Contacts API. If you're authorizing via OAuth you're probably currently asking for permission to access the gmail scope:
You will also need to ask for permission for the contacts scope:
Once you have that, you can make a GET request similar to the following:
This should return a bunch of xml, here are some relevant bits:
As you can see you can find the authorized users' email in a couple of places.
If you're using OAuth you might also want to have a look at the Google OAuth playground, I've found it very handy: http://googlecodesamples.com/oauth_playground/index.php. If you decide to use OAuth2 there is equivalent tool at https://code.google.com/oauthplayground
skorks 的答案工作得很好,但你应该使用正确的 API。通过添加额外的范围,
您可以“以正确的方式”做到这一点!
我用示例代码写了一篇关于此的完整文章: http ://www.hackviking.com/2013/10/python-get-user-info-after-oauth/
代码可在此处获取:https://code.google.com/p/google- api-oauth-演示/
skorks answer works just fine but you should use the correct API. By adding additional scope of
You do it "the right way"!
I wrote a complete article about this with example code: http://www.hackviking.com/2013/10/python-get-user-info-after-oauth/
Code available here: https://code.google.com/p/google-api-oauth-demo/