有没有办法在使用 Oauth 进行 Gmail 身份验证后获取您的电子邮件地址?

发布于 2024-11-28 10:20:54 字数 741 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫罗兰の梦幻 2024-12-05 10:20:54

您需要的是 Google Contacts API。如果您通过 OAuth 授权,您当前可能正在请求访问 gmail 范围的权限:

https://mail.google.com/mail/feed/atom

您还需要请求联系人范围的权限:

https://www.google.com/m8/feeds/

获得权限后,您可以发出类似于以下内容的 GET 请求:

https://www.google.com/m8/feeds/contacts/default/full?max-results=1

这应该返回一堆 xml,以下是一些相关位:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/"CEcMRX0_eCp7ImA9WhdRF00."">
<id>[email protected]</id>
...
<author>
  <name>Joe Bloggs</name>
  <email>[email protected]</email>
</author>
...
</feed>

正如您所看到的,您可以在几个地方找到授权用户的电子邮件。

如果您正在使用 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:

https://mail.google.com/mail/feed/atom

You will also need to ask for permission for the contacts scope:

https://www.google.com/m8/feeds/

Once you have that, you can make a GET request similar to the following:

https://www.google.com/m8/feeds/contacts/default/full?max-results=1

This should return a bunch of xml, here are some relevant bits:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/"CEcMRX0_eCp7ImA9WhdRF00."">
<id>[email protected]</id>
...
<author>
  <name>Joe Bloggs</name>
  <email>[email protected]</email>
</author>
...
</feed>

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

陪你到最终 2024-12-05 10:20:54

skorks 的答案工作得很好,但你应该使用正确的 API。通过添加额外的范围,

https://www.googleapis.com/auth/userinfo.email

您可以“以正确的方式”做到这一点!

我用示例代码写了一篇关于此的完整文章: 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

https://www.googleapis.com/auth/userinfo.email

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文