使用 Google API 仅获取用户电子邮件的最佳方式是什么?
What is the best way to use the Google API to get just a user's email?
我正在用 gmail api 编写一个程序,我 运行 遇到的一个问题是获取当前授权用户的电子邮件地址。我想我可以像在 http://www.hackviking.com/development/python-get-user-info-after-oauth/ 中那样使用范围 https://www.googleapis.com/auth/userinfo.email
来做到这一点,但是当我在 Google 的 OAuth Playground 中尝试这样做时,它请求 "know who you are on Google" 的许可以及 "know who you are on Google" 的许可查看您的电子邮件地址。该程序不需要知道用户的 name/info 等,它只需要电子邮件地址。
我什至尝试使用 userinfo.email 范围的原因是,在查看了一段时间后,我无法在 gmail 范围内找到获取用户电子邮件的方法,但如果有一种方法可以会更好
您可以使用 getProfile 方法,为此您需要以下范围之一:
https://mail.google.com/
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/gmail.compose
https://www.googleapis.com/auth/gmail.readonly
然后你就可以得到电子邮件地址:
请求
GET https://www.googleapis.com/gmail/v1/users/me/profile?fields=emailAddress&access_token={ACCESS_TOKEN}
回应
{
"emailAddress": "foo@gmail.com"
}
在 Python 中看起来像:
profile = gmail_service.users().getProfile(userId='me').execute()
print 'Email address of user is: %s' % profile['emailAddress']
我正在用 gmail api 编写一个程序,我 运行 遇到的一个问题是获取当前授权用户的电子邮件地址。我想我可以像在 http://www.hackviking.com/development/python-get-user-info-after-oauth/ 中那样使用范围 https://www.googleapis.com/auth/userinfo.email
来做到这一点,但是当我在 Google 的 OAuth Playground 中尝试这样做时,它请求 "know who you are on Google" 的许可以及 "know who you are on Google" 的许可查看您的电子邮件地址。该程序不需要知道用户的 name/info 等,它只需要电子邮件地址。
我什至尝试使用 userinfo.email 范围的原因是,在查看了一段时间后,我无法在 gmail 范围内找到获取用户电子邮件的方法,但如果有一种方法可以会更好
您可以使用 getProfile 方法,为此您需要以下范围之一:
https://mail.google.com/
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/gmail.compose
https://www.googleapis.com/auth/gmail.readonly
然后你就可以得到电子邮件地址:
请求
GET https://www.googleapis.com/gmail/v1/users/me/profile?fields=emailAddress&access_token={ACCESS_TOKEN}
回应
{
"emailAddress": "foo@gmail.com"
}
在 Python 中看起来像:
profile = gmail_service.users().getProfile(userId='me').execute()
print 'Email address of user is: %s' % profile['emailAddress']