【问题标题】:I keep getting authentication errors with the Google API我不断收到 Google API 的身份验证错误
【发布时间】:2021-08-19 03:49:43
【问题描述】:

我一直在尝试制作一个脚本,用视频观看量来更新 YouTube 标题,例如 Tom Scott 视频。但我在身份验证时不断收到此错误。

我得到了验证它的代码,但它只是说我没有足够的权限。

<HttpError 400 when requesting https://youtube.googleapis.com/youtube/v3/videos?part=%28%27snippet%2Cstatus%2Clocalizations%27%2C%29&alt=json returned "'('snippet'". Details: "[{'message': "'('snippet'", 'domain': 'youtube.part', 'reason': 'unknownPart', 'location': 'part', 'locationType': 'parameter'}]"> File "C:\Users\erik\Documents\PYTHON CODE\view_changer\example.py", line 49, in main response_update = request_update.execute().

这是我的代码:

import os

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

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


video_1_part_view = 'statistics'
video_1_id_view = 'hVuoPgZJ3Yw'
video_1_part_update= "snippet,status,localizations",
video_1_part_body_update= { "id": "1y94SadSsMU",
                            "localizations": {"es": {    "title": "no hay nada a ver aqui",
                                                         "description": "Esta descripcion es en español."
                                                        }
                                                }
                            }

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 = (r"C:\Users\erik\Documents\PYTHON CODE\view_changer\client_secret_379587650859-bbl63je7sh6kva19l33auonkledt09a9.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)

    #check views
    request_view= youtube.videos().list( part = video_1_part_view, id = video_1_id_view )
    respons_view = request_view.execute()
    views = respons_view['items'][0]['statistics']['viewCount']
    print (views)
   
    #update video
    request_update = youtube.videos().update(part= video_1_part_update, body = video_1_part_body_update)
    response_update = request_update.execute()
    print(response_update)

main()
# if __name__ == "__main__":
#     main()

如果问题不在代码中,我可能会有其他想法。当我第一次设置 OAuth 时,我不知道我需要选择用户将授予权限的内容(我知道菜鸟错误(: ),但由于我更新了它,当我在访问后请求权限时验证链接代码返回给我,它不会要求我告诉它做的所有事情的许可。

【问题讨论】:

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


    【解决方案1】:

    根据官方文档,Videos.list API端点的请求参数part定义为:

    part(字符串)

    part 参数指定 API 响应将包含的一个或多个 video 资源属性的逗号分隔列表。

    [...]

    但是您将请求参数设置为:

    ('snippet,status,localizations',).

    那是因为(尽管您上面的代码没有显示)您将变量 video_1_part_view 定义为:

    video_1_part_view = "snippet,status,localizations",
    

    (请注意结尾的逗号)。这使得 video_1_part_view 成为 Python tuple,这不是您需要发送到 API 端点的内容。

    变量video_1_part_update也是如此。

    只需删除结尾的逗号,一切都会正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-24
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      相关资源
      最近更新 更多