【问题标题】:Get user info from YouTube Google API in Python (API v3)在 Python (API v3) 中从 YouTube Google API 获取用户信息
【发布时间】:2014-11-16 19:34:58
【问题描述】:

我是 Google API 的新手,但取得了一些进展;我有一个 Django 应用程序正在获取用户的视频上传列表。这很好用!我从 Google 提供的文档和示例中获取了所有内容。

我还希望能够获取与他们获得授权的 YouTube 帐户相关联的用户个人资料信息(因为我网站上的用户可能拥有多个 YouTube 帐户,我需要将它们区分开来)。

这是我正在使用的简化版本:

YOUTUBE_READONLY_SCOPE = "https://www.googleapis.com/auth/youtube.readonly"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

# added this to increase the scope
GOOGLE_USER_INFO_SCOPE = "https://www.googleapis.com/auth/userinfo.profile"

FLOW = flow_from_clientsecrets(
    CLIENT_SECRETS,
    scope=[ YOUTUBE_READONLY_SCOPE, GOOGLE_USER_INFO_SCOPE ],
    redirect_uri='http://127.0.0.1:8000/youtube/oauth2callback')

@login_required
def index(request):
    storage = Storage(CredentialsModel, 'id', request.user, 'credential')
    credential = storage.get()

    http = httplib2.Http()
    http = credential.authorize(http)

    # gets a workable YouTube service!
    service = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=http)

    # how do I get the user name or email or other profile information here?

阅读this post 后,我知道我不能只使用userinfo = build('userinfo', "v2", http=http) ...但我对接下来要做什么有点困惑。我阅读了this example,但它似乎使用了不同的库(我安装了google-api-python-client)。

我是否需要安装不同的库(gdata 库)才能获取用户信息?或者用户信息是否隐藏在我可以调用 build 的其他服务中(如 Google+ 服务)?

【问题讨论】:

  • 你是对的。您必须建立与 Google+ API 的连接才能获取用户信息。
  • 我被带到 Google 登录页面,但我被重定向到带有以下链接的页面 - 127.0.0.1:8000/accounts/google/login/callback/… 此页面返回 404。任何提示?如果可能的话,我可以在 Github 或其他任何地方获取您的完整代码或链接吗?

标签: python django google-api youtube-api


【解决方案1】:

因此必须添加一个额外的范围,它是 Google Plus API 的一部分。这是我添加的范围:

GOOGLE_PLUS_SCOPE = "https://www.googleapis.com/auth/plus.me"
GOOGLE_PLUS_SERVICE_NAME = "plus"
GOOGLE_PLUS_VERSION = "v1"

这是我如何同时做两个范围的:

FLOW = flow_from_clientsecrets(
    CLIENT_SECRETS,
    scope=[ GOOGLE_PLUS_SCOPE, YOUTUBE_READONLY_SCOPE ],
    redirect_uri='http://127.0.0.1:8000/youtube/oauth2callback')

最后,这是我获得登录用户信息的方式:

userservice = build(GOOGLE_PLUS_SERVICE_NAME, GOOGLE_PLUS_VERSION, http=http)
userpeople = userservice.people()
me = userpeople.get(userId="me").execute()

print(me)

需要指出的一点是,我必须启用 Google+ API 以及联系人 API。我不知道为什么,但在启用 Contacts API 之前这不起作用。

【讨论】:

    猜你喜欢
    • 2018-07-15
    • 2015-04-14
    • 2018-04-26
    • 2015-08-10
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    相关资源
    最近更新 更多