【问题标题】:Google OAuth2 using Python 3 to access YouTube APIGoogle OAuth2 使用 Python 3 访问 YouTube API
【发布时间】:2018-05-26 17:27:07
【问题描述】:

我在使用 Python 在 OAuth2 中做任何事情时都遇到了麻烦。我已经经历了来自多个站点的示例代码的大约 20 次不同的迭代,但它们都不起作用。这是我此时文件中的内容,但无法编译。有什么建议?提前致谢!

import requests
from requests_oauthlib import OAuth2Session
from requests.auth import HTTPBasicAuth
from oauthlib.oauth2 import BackendApplicationClient

client_id = "xxx"
client_secret = "yyy"
authorization_base_url = 'https://accounts.google.com/o/oauth2/auth'
token_url = 'https://accounts.google.com/o/oauth2/token'
project_id = 'zzz'
redirect_uri = "http://localhost"

client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url=token_url, client_id=client_id,
    client_secret=client_secret)

【问题讨论】:

    标签: python oauth-2.0 google-api youtube-api google-oauth


    【解决方案1】:

    您可以使用Python Quickstart 作为参考,因为它与 Python 2.6 或更高版本兼容。

    别忘了,您需要一个本地网络服务器来测试它,以便它可以连接到互联网。您可以使用类似

    python -m SimpleHTTPServer 8080
    

    【讨论】: