首页 > 解决方案 > 使用 YouTube 数据 API 时出现授权错误

问题描述

我正在尝试使用 YouTube Data API v3,但是当我运行我的脚本时出现以下错误:

这是我正在使用的脚本:

import os

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

from googleapiclient.http import MediaFileUpload

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

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 = "client_secret.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.thumbnails().set(
        videoId="EoOUcFHsE9w",
        
        # TODO: For this request to work, you must replace "YOUR_FILE"
        #       with a pointer to the actual file you are uploading.
        media_body=MediaFileUpload("300coin.png")
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

当我运行这个脚本时,我在控制台中得到了这个:

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=763961784735-ti8rm4futst2u81hdmnaf803e9gcfm82.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&state=NQWjaXU1VCruBsePLHo0M8U6eDCsoF&prompt=consent&access_type=offline
Enter the authorization code: 

当我点击链接时,会打开一个浏览器并显示以下通知:

Error 400: redirect_uri_mismatch
The redirect URI in the request, urn:ietf:wg:oauth:2.0:oob, can only be used by a Client ID for native application. It is not allowed for the WEB client type. You can create a Client ID for native application at

在我用于授权 JavaScript 来源的 Web 应用程序的 ClientId 中:http://localhost:8080. 对于授权的重定向 URI:http://localhost:8080.

当我去域验证并想添加 localhost 时,我必须写http://www.localhost:8080才能获得谷歌验证 .html 文件,但不用说,域http://www.localhost:8080不起作用。那么我该如何解决这个问题呢?

我想要的只是运行一个脚本并在 Python 中更新我的缩略图。

标签: python-3.xyoutube-apiyoutube-data-api

解决方案


推荐阅读