我目前正在使用 Python 构建一个与 Google API 交互的网络应用程序。使用 oauth 获取对用户资源的访问权限。像这样成功验证和升级 token 后:
gd_client = gdata.photos.service.PhotosService()
gd_client.SetAuthSubToken(token)
gd_client.UpgradeToSessionToken()
然后我可以访问 API 的不同提要并获取用户 Youtube 视频列表等。但是用户只使用谷歌登录,而我只有一个 oauth token ,没有关于用户的其他信息。如何检索用户信息?比如电子邮件、显示名称等?我一直在测试很多不同的东西而没有设法解决这个问题......
我在这里发现了一些有趣的东西:Is there a way to get your email address after authenticating with Gmail using Oauth?
我的理论是我可以使用 PhotoService.GetAuthSubToken() 然后重用该 token 来请求联系人并从联系人条目中获取 auther.email。将授权范围更改为:
scope = ['https://picasaweb.google.com/data/', 'https://www.google.com/m8/feeds/']
女巫返回对两种服务都有效的 roken...有什么想法吗?
最佳答案
我只想添加一个我发现特别容易使用的资源。这是:link .
Kallsbo 通过搜索范围 https://www.googleapis.com/auth/userinfo.email
将我引导到正确的位置。在您已经拥有 credentials
之后,只需使用直接从该链接获取的以下函数:
def get_user_info(credentials):
"""Send a request to the UserInfo API to retrieve the user's information.
Args:
credentials: oauth2client.client.OAuth2Credentials instance to authorize the
request.
Returns:
User information as a dict.
"""
user_info_service = build(
serviceName='oauth2', version='v2',
http=credentials.authorize(httplib2.Http()))
user_info = None
try:
user_info = user_info_service.userinfo().get().execute()
except errors.HttpError, e:
logging.error('An error occurred: %s', e)
if user_info and user_info.get('id'):
return user_info
else:
raise NoUserIdException()
将其命名为 user_email = get_user_info(credentials)['email']
,您的电子邮件就已经存在了! :)
关于python - 如何使用 Google API Python 客户端在 OAUTH 之后获取用户电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473250/