首页 > 解决方案 > 为什么将 youtube api 与 oauth 一起使用时出现授权错误

问题描述

我正在尝试使用 youtube api 在我的帐户中添加创建播放列表。我已经创建了我的 oauth 凭据,我没有更改任何设置同意屏幕的内容。我已经下载了秘密的 json 文件。

文档中,下面是您创建新播放列表的方法:

# -*- coding: utf-8 -*-

# Sample Python code for youtube.playlists.insert
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python

import os

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

scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]

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 = "youtube"
    api_version = "v3"
    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 = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.playlists().insert(
        part="snippet,status",
        body={
          "snippet": {
            "title": "Sample playlist created via API",
            "description": "This is a sample playlist description.",
            "tags": [
              "sample playlist",
              "API call"
            ],
            "defaultLanguage": "en"
          },
          "status": {
            "privacyStatus": "private"
          }
        }
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

我在没有改变任何东西的情况下运行了它,我就是这样"Please visit this URL to authorize this application"

当我单击它时,我得到一个带有以下错误的新选项卡:

Authorization Error
Error 400: invalid_request
Missing required parameter: redirect_uri

有时,执行我得到的完全相同的事情:

Authorization Error
Error 400: invalid_request
Missing required parameter: client_id

错误中提到的缺少的必需参数似乎已填写在授权的 url 中。

任何想法发生了什么?

标签: pythonapiyoutube

解决方案


推荐阅读