【问题标题】:YT Data API throws Error 403 for just one videoYOUTUBE Data API 仅针对一个视频引发错误 403
【发布时间】:2020-06-25 20:50:51
【问题描述】:

我使用 YouTube Data API v3 将当前观看次数放入我的视频标题中,就像在这个视频 (https://www.youtube.com/watch?v=BxV14h0kFs0) 中一样,它有效!

我在 pythonanywhere 中为这个视频 ID:7vSw7XRivWY 设置了它,它就像一个魅力,还在运行。 然后我尝试对另一个视频(ID:fDh-qcbIxtU)做同样的事情,现在它抛出了

googleapiclient.errors.HttpError: https://www.googleapis.com/youtube/v3/videos?part=snippet&alt=json 返回“请求元数据无效。”

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "errors": [
      {
        "message": "The request is missing a valid API key.",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}

这个错误。我使用与另一个 ID 完全相同的代码,我所做的只是将视频 ID 的变量更改为另一个。

我不知道为什么会这样。我重试了多次,我尝试了不同的 API 范围,我尝试了另一个 API 客户端密钥 - 结果相同,不起作用。

在我的 python 代码中,我实现了一个 change_title 方法,该方法将 apiclient 和视频 ID 作为参数,然后首先获取视图计数和 sn-p 部分(其中包含标题),编辑 sn-p 的标题部分,然后相应地更新视频。

如前所述,它适用于我的任何其他视频,但不适用于这个(ID:fDh-qcbIxtU)。我 100% 确定我输入了正确的 ID,并且没有更改任何其他内容。

这是正在执行的整个文件:

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

# Sample Python code for youtube.channels.list
# 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

import time

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


def like_video(youtube, videoID):
    youtube.videos().rate(
        id=videoID,
        rating='like'
    ).execute()


def change_title(youtube, videoID):
    video_list_response_statistics = youtube.videos().list(
        id=videoID,
        part="statistics"
    ).execute()

    videos_list_statistics = video_list_response_statistics['items'][0]['statistics']

    views = videos_list_statistics['viewCount']
    print("Views: " + str(views))

    snippetPart = youtube.videos().list(
        id=videoID,
        part="snippet"
    ).execute()

    # Since the request specified a video ID, the response only contains one
    # video resource. This code extracts the snippet from that resource.
    videos_list_snippet = snippetPart['items'][0]['snippet']
    print(videos_list_snippet)
    videos_list_snippet['title'] += " [" + str(views) + " Views]"

    videos_update_response = youtube.videos().update(
        part='snippet',
        body=dict(
            id=videoID,
            snippet=videos_list_snippet
        )).execute()


def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.

    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "0"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "credentials.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)

    i = 1

    videoID = "fDh-qcbIxtU"
    while True:
        print("Update No" + str(i))
        response = change_title(youtube, videoID)
        print(response)
        i += 1
        time.sleep(1000)


if __name__ == "__main__":
    main()

感谢任何花时间尝试解决我的问题的人。

【问题讨论】:

  • 可以分享一下相关代码吗?我不确定我们能做多少。
  • 当然,请稍等片刻,让我找到编辑问题的选项
  • 添加了代码。 credentials.json 包含客户端密码和 ID 等,并且 100% 保证正确,因为它与其他视频 ID 完美配合。感谢您抽出宝贵时间尝试解决我的问题!
  • 我觉得奇怪的是,当您将 oauth2 与已安装的应用程序一起使用时,它会要求提供 api 密钥。
  • @DalmTo 我不确定我是否理解正确,但我认为客户端 id + secret 将请求标识为来自我的项目。但是,当我运行程序时,它首先要求我登录,接受我授予程序某些权限(管理 yt 帐户),然后我会得到一个随机字符的长代码,我需要在程序继续之前将其插入到程序中。跨度>

标签: python youtube youtube-api youtube-data-api


【解决方案1】:

我的一个特定 youtube 频道出现此错误,这是因为它是由品牌帐户而不是其所有者帐户管理的。

这可能对您的情况没有帮助,但也许可以帮助其他面临此问题的人。

只需使用正确的帐户进行授权。

【讨论】:

    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 2013-10-16
    • 2013-06-30
    • 2015-12-12
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多