【发布时间】: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