【问题标题】:Youtube data api v3 does not allow access to playlists, scope issue?Youtube data api v3 不允许访问播放列表,范围问题?
【发布时间】:2022-07-28 13:45:59
【问题描述】:

我有一个视频列表,我想在 youtube api v3 的帮助下添加到播放列表中。我在我的代码以及开发人员控制台中设置了 oauth:

# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/code-samples#python

import os

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

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

# 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_xxx.apps.googleusercontent.com.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)

然后我使用此功能将视频添加到我的播放列表中:

def add_video_to_playlist(youtube,videoID,playlistID):
  add_video_request=youtube.playlistItems().insert(
  part=\"snippet\",
  body={
        \'snippet\': {
          \'playlistId\': playlistID, 
          \'resourceId\': {
                  \'kind\': \'youtube#video\',
              \'videoId\': videoID
            }
        #\'position\': 0
        }
}
 ).execute()

我的开发者帐户中的范围已设置: Screenshot

我收到的错误消息如下:

HttpError: <HttpError 403 when requesting https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&alt=json returned \"Forbidden\". Details: \"[{\'message\': \'Forbidden\', \'domain\': \'youtube.playlistItem\', \'reason\': \'playlistItemsNotAccessible\'}]\">

我在哪里做错了什么?

  • documentation 中的 playlistItemsNotAccessible 错误说:The request is not properly authorized to retrieve the specified playlist. = 播放列表是否存在? - 我没有在你的截图中看到googleapis.com/auth/youtube.force-ssl...检查this answer
  • 播放列表存在并且 force-ssl 在屏幕截图中。由于您的链接,仍然发现了问题,请参见下文。

标签: python youtube-api youtube-data-api google-developers-console


【解决方案1】:

在这里回答我自己的问题: 您在开发者控制台中设置的范围也需要在创建凭证时添加。我必须做的就是改变

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

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

【讨论】:

  • 我很高兴你解决了这个问题:)
猜你喜欢
  • 2013-09-19
  • 2017-11-01
  • 2014-07-02
  • 2022-01-15
  • 1970-01-01
  • 2016-09-17
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多