如何将youtube api auth和权限响应从服务器传递到客户端的webrowser

问题描述:

因此,我有一个正在服务器上运行的Python脚本(PHP运行它)。但是,脚本必须在YouTube上验证用户身份。当您运行localy时,它会打开浏览器,允许进行身份验证并请求权限。一切正常。当我允许用户在服务器上运行它时,它将尝试打开服务器上的验证屏幕。我需要将响应传递给Web浏览器。 任何想法? 我使用AUTH脚本通过YouTube API的例子:如何将youtube api auth和权限响应从服务器传递到客户端的webrowser

# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains 
# the OAuth 2.0 information for this application, including its client_id and 
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from 
# the Google Developers Console at 
# https://console.developers.google.com/. 
# Please ensure that you have enabled the YouTube Data API for your project. 
# For more information about using OAuth2 to access the YouTube Data API, see: 
# https://developers.google.com/youtube/v3/guides/authentication 
# For more information about the client_secrets.json file format, see: 
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets 
CLIENT_SECRETS_FILE = "client_secrets.json" 

# This variable defines a message to display if the CLIENT_SECRETS_FILE is 
# missing. 
MISSING_CLIENT_SECRETS_MESSAGE = """ 
WARNING: Please configure OAuth 2.0 

To make this sample run you will need to populate the client_secrets.json file 
found at: 

    %s 

with information from the Developers Console 
https://console.developers.google.com/ 

For more information about the client_secrets.json file format, please visit: 
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets 
""" % os.path.abspath(os.path.join(os.path.dirname(__file__), 
            CLIENT_SECRETS_FILE)) 

# This OAuth 2.0 access scope allows for full read/write access to the 
# authenticated user's account. 
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube" 
YOUTUBE_API_SERVICE_NAME = "youtube" 
YOUTUBE_API_VERSION = "v3" 

flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, 
    message=MISSING_CLIENT_SECRETS_MESSAGE, 
    scope=YOUTUBE_READ_WRITE_SCOPE) 

storage = Storage("%s-oauth2.json" % sys.argv[0]) 
credentials = storage.get() 

if credentials is None or credentials.invalid: 
    flags = argparser.parse_args() 
    credentials = run_flow(flow, storage, flags) 

youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, 
    http=credentials.authorize(httplib2.Http())) 

服务器端Web应用程序是安静简单,它允许选择脚本变化和情侣的设置,仅此而已。

+0

有用户验证它第一次在网络上谷歌将返回一个刷新令牌保存刷新令牌,当您在服务器上运行它时,您可以使用刷新令牌访问用户帐户 – DaImTo

当你在你的服务器上验证你的用户凭证时,你需要使用Oauth2(在谷歌的情况下)。
我花了一些时间了解它在开始以及...
OAuth2的含义是,您将用户重定向到由google提供的登录地址,告诉服务“看,用户登录后,发送URL X的标记“(您的服务器)。您的服务器获取某种代表用户授权您的“应用程序”代表他执行任务的令牌。

退房https://developers.google.com/api-client-library/python/auth/web-app

特别是在你的情况 - 你需要实现端点在PHP(它需要,如果你让我的漂移上的地址为“听”)

如果你不并非真正需要用户凭证,只需要以任何经过验证的用户身份访问YouTube API,这很容易 - 您需要拥有自己的凭据并执行以下解释:“服务器到服务器”: https://developers.google.com/api-client-library/python/auth/service-accounts

+0

我的应用将操纵用户播放列表,所以我想我需要验证。感谢你的回答。认为这会帮助我很多: https://developers.google.com/api-client-library/python/auth/web-app#protectauthcode – Kraxi