根据Google的OAUTH API documentation,我们已弃用userinfo.profile
和userinfo.email
范围,转而使用profile
和email
。其他API用户也有很多关于此开关的information。
但是,在尝试使用People API时,我收到此错误:
2016-02-22 13:01:25,044 :Exception on /admin/testbank/settings [GET]
Traceback (most recent call last):
(Flask traceback omitted...)
File "/home/somedev/testweb/views/admin_view.py", line 78, in admin_view
res_acct_info = people_service.people().get(userId='me').execute()
File "/home/somedev/env/lib/python3.4/site-packages/googleapiclient/discovery.py", line 676, in method
raise TypeError('Got an unexpected keyword argument "%s"' % name)
TypeError: Got an unexpected keyword argument "userId"
查看Google API sample,这应该是正确的电话。但是,在库discovery.py
中转储parameters.argmap
会显示userId不存在。我做错了什么?
(注意:我正在尝试标记google-people
,因为这是API页面建议的位置,但我没有足够的代表来标记此内容。还有其他人可以添加此标记吗?我?)
答案 0 :(得分:1)
事实证明,我对我正在阅读的确切代码视而不见......所有这些示例(尤其是Google example)都使用了Google+ API ,确实有userId参数。
供参考,"旧方式"是使用oauth2 / userinfo服务:
service = build('oauth2', 'v2', http=http)
user = users_service.userinfo().get().execute()
name = user.get('name')
email = user.get('email')
您可以使用Google+ API 获取相同的信息 - 即使用户没有Google + ,也可以:
service = discovery.build("plus", "v1", http=http)
user = service.people().get(userId='me').execute()
# This assumes that user['emailAddresses'] exists and
# has at least one element...
name = user.get('displayName')
email = user.get('emailAddresses')[0].get("value")
在撰写本文时,People API似乎是released recently(2016年2月10日)!有意义的是,关于它的文档不会很多......
要使用较新的People API (可能声称来自Google+的清洁度),这是获取当前用户信息的正确方法:
service = discovery.build('people', 'v1', http_auth)
user = people_service.people().get(resourceName='people/me').execute()
# This assumes that user['names'] and user['emailAddresses']
# exists and has at least one element...
name = user.get('names')[0].get("displayName")
email = user.get('emailAddresses')[0].get("value")
resourceName
取代了People API中的userId
。它具有类似的目的(用于标识当前用户或其他用户),但具有不同的格式,如使用'people/me'
与仅使用'me'
一样。
Google+和较新的People API都只需要email
和profile
范围。但是,与之前的userinfo
API不同,您需要手动启用Google+ API和/或People API才能使用它们。
tl; dr :Google+ API使用userId='me'
,新的People API使用resourceName='people/me'
,您应该使用这些受支持的API之一 - 两者都返回相同的信息,仅在格式略有不同!