【问题标题】:Issues with handling Oauth2.0 to add a video to a YouTube playlist, adding videos continuously处理 Oauth2.0 将视频添加到 YouTube 播放列表,不断添加视频的问题
【发布时间】:2020-09-08 00:47:54
【问题描述】:

我正在用 Python 编写代码来自动将视频添加到 YouTube 播放列表。该代码确实成功地将视频添加到播放列表,但是对于我添加的每个视频,都会提示我登录同意屏幕。对于我添加的每个视频,身份验证流程都会重复。我希望只做一次。

def insertPlaylistItem(playlistId, videoID):   
    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret.json"

    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
    credentials = flow.run_console() # Save these credentials
    youtube = googleapiclient.discovery.build(api_service_name, api_version, credentials=credentials)

    request = youtube.playlistItems().insert(
            part="snippet",
            body={
                "snippet": {
                    "playlistId": playlistId,
                    "position": 0,
                  "resourceId": {
                    "kind": "youtube#video",
                    "videoId": videoID
                }
                }
            }
        )
    response = request.execute()

    print(response)

client_secret.json 是这种形式:

{
    "installed":{
        "client_id":"XXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
        "project_id":"XXXXXX",
        "auth_uri":"https://accounts.google.com/o/oauth2/auth",
        "token_uri":"https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
        "client_secret":"XXXXXXXXXXXXXXXXXXXXXXX",
        "redirect_uris":[
            "urn:ietf:wg:oauth:2.0:oob",
            "http://localhost"
            ]
    }
}

有没有办法存储身份验证流程提供的这些凭据,以便以后使用?我已经通读了https://developers.google.com/youtube/v3/docs/playlists/insert,在那里我让它适用于javascript,但我只是不太明白Python的方式。我是不是对 Oauth2.0 流程理解有误?

使用 javascript,我能够以如下形式获得 token.json:

{
    "access_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "refresh_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "scope":"https://www.googleapis.com/auth/youtube.force-ssl",
    "token_type":"Bearer",
    "expiry_date":1564674820338
}

【问题讨论】:

    标签: python python-3.x youtube-api google-oauth


    【解决方案1】:

    解决了!

    我最终使用了 Python 的 oauth2client 模块。由于 oauth2.json 文件的存储方式不同,我还必须使用该模块的流程而不是 Google 的流程。使用 Google 的流程,我不断收到:KeyError: '_module'。

    解决办法是:

    import googleapiclient.discovery
    import googleapiclient.errors
    from oauth2client.client import flow_from_clientsecrets
    from oauth2client.file import Storage
    from oauth2client.tools import run_flow
    
    def insertPlaylistItem(playlistId, videoID):
        api_service_name = "youtube"
        api_version = "v3"
        client_secrets_file = "client_secret.json"
    
        flow = flow_from_clientsecrets(client_secrets_file, scopes)
        storage = Storage("oauth2.json")
        credentials = storage.get()
        if credentials is None or credentials.invalid:
            credentials = run_flow(flow, storage)
            storage.put(credentials)
    
        youtube = googleapiclient.discovery.build(api_service_name, api_version, credentials=credentials)
    
        request = youtube.playlistItems().insert(
            part="snippet",
            body={
                "snippet": {
                    "playlistId": playlistId,
                    "position": 0,
                    "resourceId": {
                        "kind": "youtube#video",
                        "videoId": videoID
                    }
                }
            }
        )
        response = request.execute()
        print(response)
    

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 2014-02-09
      • 2015-12-31
      • 1970-01-01
      • 2019-06-13
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多