I'm currently building a web app interacting with Google API in Python. Using the oauth to get access to the users resources. After successful authentication and upgrade of the token like this:
我目前正在构建一个与Python中的Google API交互的Web应用程序。使用oauth访问用户资源。成功验证和升级令牌后,如下所示:
gd_client = gdata.photos.service.PhotosService()
gd_client.SetAuthSubToken(token)
gd_client.UpgradeToSessionToken()
Then I can access the different feeds of the API and get a list of the users Youtube videos for example. But the user has only logged in with Google and all I have is a oauth token and no other info on the user. How do I retrieve info on the user? Like email, display name and so on? I have been testing a lot of different stuff without managing to solve this...
然后,我可以访问API的不同供稿,并获取用户Youtube视频的列表。但是用户只使用Google登录,我所拥有的只是一个oauth令牌而没有关于用户的其他信息。如何检索用户的信息?像电子邮件,显示名称等?我一直在测试很多不同的东西而没有设法解决这个问题......
I found some interesting here: Is there a way to get your email address after authenticating with Gmail using Oauth?
我在这里发现了一些有趣的内容:使用Oauth通过Gmail验证后,有没有办法获取您的电子邮件地址?
My theory was that I could use the PhotoService.GetAuthSubToken() and then reuse that token to request the contact and get auther.email from the contact entry. Changed the scope of the auth to:
我的理论是我可以使用PhotoService.GetAuthSubToken()然后重用该令牌来请求联系并从联系人条目获取auther.email。将auth的范围更改为:
scope = ['https://picasaweb.google.com/data/', 'https://www.google.com/m8/feeds/']
witch returns a roken valid for both services... Any ideas?
女巫回归有效的两种服务...任何想法?
2 个解决方案
#1
7
So I found a great way to do it!
所以我发现了一个很好的方法!
Request the extra scope of https://www.googleapis.com/auth/userinfo.email then I can access that with a Gdata.Client to get the e-mail address.
请求https://www.googleapis.com/auth/userinfo.email的额外范围,然后我可以使用Gdata.Client访问该范围以获取电子邮件地址。
Complete example code: https://code.google.com/p/google-api-oauth-demo/
完整的示例代码:https://code.google.com/p/google-api-oauth-demo/
Complete write up of how I got there: http://www.hackviking.com/2013/10/python-get-user-info-after-oauth/
完整写下我如何到达那里:http://www.hackviking.com/2013/10/python-get-user-info-after-oauth/
#2
6
I just want to add a resource I found to be particularly easier to use. Here it is: link. Kallsbo directed me to a correct location by searching for scope https://www.googleapis.com/auth/userinfo.email
. After you already have credentials
, simply use the following function taken directly from that link:
我只是想添加一个我发现特别容易使用的资源。这是:链接。 Kallsbo通过搜索范围https://www.googleapis.com/auth/userinfo.email将我带到了正确的位置。在您拥有凭据之后,只需使用直接从该链接获取的以下函数:
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()
Call it user_email = get_user_info(credentials)['email']
, and you already have your email there! :)
称之为user_email = get_user_info(凭据)['email'],您已经在那里收到了电子邮件! :)
#1
7
So I found a great way to do it!
所以我发现了一个很好的方法!
Request the extra scope of https://www.googleapis.com/auth/userinfo.email then I can access that with a Gdata.Client to get the e-mail address.
请求https://www.googleapis.com/auth/userinfo.email的额外范围,然后我可以使用Gdata.Client访问该范围以获取电子邮件地址。
Complete example code: https://code.google.com/p/google-api-oauth-demo/
完整的示例代码:https://code.google.com/p/google-api-oauth-demo/
Complete write up of how I got there: http://www.hackviking.com/2013/10/python-get-user-info-after-oauth/
完整写下我如何到达那里:http://www.hackviking.com/2013/10/python-get-user-info-after-oauth/
#2
6
I just want to add a resource I found to be particularly easier to use. Here it is: link. Kallsbo directed me to a correct location by searching for scope https://www.googleapis.com/auth/userinfo.email
. After you already have credentials
, simply use the following function taken directly from that link:
我只是想添加一个我发现特别容易使用的资源。这是:链接。 Kallsbo通过搜索范围https://www.googleapis.com/auth/userinfo.email将我带到了正确的位置。在您拥有凭据之后,只需使用直接从该链接获取的以下函数:
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()
Call it user_email = get_user_info(credentials)['email']
, and you already have your email there! :)
称之为user_email = get_user_info(凭据)['email'],您已经在那里收到了电子邮件! :)