【问题标题】:token issues youtube analytics api python cloud令牌问题 youtube 分析 api python 云
【发布时间】:2021-07-11 21:21:33
【问题描述】:

我得到了用于从 youtube 分析 api(不是 youtube 数据 API V3)中提取 youtube 信息的下一个代码

import os

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtubeAnalytics"
    api_version = "v2"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube_analytics = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube_analytics.reports().query(
        dimensions="video",
        endDate="2014-06-30",
        ids="channel==MINE",
        maxResults=10,
        metrics="estimatedMinutesWatched,views,likes,subscribersGained",
        sort="-estimatedMinutesWatched",
        startDate="2014-05-01"
    )
    response = request.execute()

    print(response)

我得到了客户端密码并且代码运行良好,但是我在云上运行代码(特别是 deepnote)所以在某些时候代码需要手动输入令牌,有没有办法避免这种情况,或者以某种方式拉令牌?因为我认为在云上运行此代码时无法检索令牌。

提前致谢。

【问题讨论】:

    标签: python youtube-analytics-api deepnote


    【解决方案1】:

    OAuth 流程设计为交互式。它通常用于您(应用程序/Web 开发人员)希望用户单击链接以使用自己的帐户登录的情况。这通常需要重定向、弹出窗口或输入字段(如在 Colab 或 Jupyter 中),它们在其中进行此类交互。

    执行此类自动请求的正确方法是通过不同类型的 API,或使用不同类型的身份验证。在许多 GCP API 的情况下,您会使用服务帐户,但这个特定的 API does not support service account authentication

    如果你真的想使用 OAuth 客户端密码流,你可以在 Deepnote 中进行,当你运行 flow.run_console() 命令时,你会得到一个输入,你必须输入你的(用户)谷歌批准的令牌帐户。但是,要使其正常工作,您必须创建一个桌面应用程序客户端密码,并允许您的用户访问测试模式,然后单击 OAuth 警告。或者您必须发布 OAuth 应用程序,这需要 Google 的批准。

    在这里,我发布了一个示例 Deepnote 笔记本,表明这是可能的:https://deepnote.com/@jz/YouTube-Analytics-API-vuA9wCgGRKiGN9Mjaoi54Q

    但这需要您可能不想要的手动交互。在其他 GCP API 中,您可以像这样使用您的服务帐号:

    credentials = service_account.Credentials.from_service_account_file(service_account_file, scopes=scopes)
    
    

    因此,如果您真的需要自动化,则需要使用不同的 YouTube API。

    【讨论】:

      【解决方案2】:

      您可以保存您的凭据,这将使您的进一步流程自动化。使用这个thread链接我已经解释过了。

      【讨论】:

        猜你喜欢
        • 2012-12-05
        • 1970-01-01
        • 2013-08-27
        • 2011-09-11
        • 2022-08-06
        • 2013-05-03
        • 2017-08-11
        • 1970-01-01
        • 2011-05-13
        相关资源
        最近更新 更多