【问题标题】:How do I get the youtube api client handle using access token如何使用访问令牌获取 youtube api 客户端句柄
【发布时间】:2015-10-30 11:09:11
【问题描述】:

我正在尝试使用 youtube 分析 API。我已经将通道的访问令牌存储在某个数据库中。在 PHP 中,您可以构建客户端并添加访问令牌、client_id、client_secret,然后使用客户端调用 youtube 分析。

然而,在 python 示例中,我看到了这样的内容:

flow = flow_from_clientsecrets(
    CLIENT_SECRETS_FILE,
    scope=' '.join(YOUTUBE_SCOPES),
    message=MISSING_CLIENT_SECRETS_MESSAGE)

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

if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage, data)

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

yt.analytics = build(
    YOUTUBE_ANALYTICS_API_SERVICE_NAME,
    YOUTUBE_ANALYTICS_API_VERSION,
    http=http)

它使用浏览器对用户进行身份验证。我不需要这一步,因为我已经将access_token 存储在数据库中。问题是如何在build() 函数调用中使用access_token,以便我可以继续下面的查询。

analytics_query_response = yt_analytics.reports().query(
    ids='channel==',
    metrics=options.metrics,
    dimensions=options.dimensions,
    start_date=options.start_date,
    end_date=options.end_date,
    max_results=options.max_results,
    sort=options.sort,
).execute()

【问题讨论】:

    标签: python youtube-api youtube-analytics-api


    【解决方案1】:

    不幸的是,build 函数没有access_token 参数。这是docs

    您也可以像这样创建您的 credentials 对象(这可能是您正在寻找的):

    from oauth2client.client import GoogleCredentials
    from oauth2client import GOOGLE_TOKEN_URI
    
    access_token = YOUR_TOKEN
    token_expiry = None
    token_uri = GOOGLE_TOKEN_URI
    user_agent = 'Python client library'
    revoke_uri = None
    
    credentials = GoogleCredentials( 
        access_token, 
        client_id,
        client_secret, 
        refresh_token, 
        token_expiry,
        token_uri, 
        user_agent,
        revoke_uri=revoke_uri
        )
    

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 1970-01-01
      • 2021-09-12
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 2014-07-18
      • 2019-11-22
      相关资源
      最近更新 更多