1

我正在尝试使用 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数据存储在数据库中。问题是如何access_tokenbuild()函数调用中使用它,以便我可以继续下面的查询。

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()
4

1 回答 1

2

不幸的是,该build函数没有access_token参数。这是文档

不过,您也可以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
    )
于 2015-08-07T18:26:16.187 回答