我正在尝试做一个与从YouTube收集数据有关的个人项目,并且使用API方法需要一个需要使用OAuth2进行身份验证的API方法。您可以使用API密钥的那些,我都可以使用,而我的小应用程序的那一部分也很棒。但是下载视频的笔录需要OAuth2。
作为找出OAuth2的起点,我在YouTube API参考页面的字幕下载方法上获得了从尝试IT侧栏生成的示例Python代码。我对其进行了修改,以包括我感兴趣的视频之一的字幕ID,我从Google Cloud Console下载的Project的OAuth 2.0客户端ID的JSON Secrets文件,以及我希望它的文件路径放成绩单。
不幸的是,它行不通!我在下面包括了我使用的代码,秘密json文件的内容以及带有错误消息的控制台的内容。
该代码包括 flow.run_console()
的调用,该访问量标记为pycharm。当我将鼠标悬停在它上几秒钟时,这会弹出有关该功能的文档上方:
New clients will be unable to use `InstalledAppFlow.run_console` starting on Feb 28, 2022. All clients will be unable to use this method starting on Oct 3, 2022. Use `InstalledAppFlow.run_local_server` instead. For details on the OOB flow deprecation, see https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html?m=1#disallowed-oob
在该站点上,我读了有关使他们贬低此问题的安全风险。不幸的是,我无法从该站点弄清楚如何更换它。使用 recortentials = flow.run_local_server()
而不是 recordentials = flow.run_console()
产生的keyError完全相同。似乎其他选项都旨在在网站上托管或用于移动应用程序。我只想专门在计算机上使用此应用。
它似乎确实可以直到该行,或者至少给了我一个URL,可以单击我,授权我的项目使用我的YouTube帐户,然后粘贴代码。它是在我粘贴到代码中失败。它给我的错误实际上是关于凭证文件,没有名称 client_secret
的键 - 实际上,我的凭据文件中缺少了该键。也许他们通常停止了其中,并且从未在API参考上更新示例代码生成器?在搜索其他答案时,我看到了很多引用使用“已安装 /其他”类型创建OAuth2客户端ID的参考,但据我所知,这不再是类型下拉菜单的选项。
或者,也许这与我的应用程序仍在“测试”模式中列出的事实有关?我不打算将其发布给世界,我是唯一一个会使用此应用程序的人。它留在我的计算机上,因此我没有网站可以提供Google,以便他们可以查看我的隐私政策或他们对已发布的应用程序所需的其他要求。
运行代码时的错误消息:
C:\Users\myUserName\PycharmProjects\MyProjectName\venv\Scripts\python.exe C:/Users/acrai/PycharmProjects/MyProjectName/example_download_captions.py
flow_before
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=xxxxxxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=urnxxxxxxoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.force-ssl&state=xxxxxxxxxx&prompt=consent&access_type=offline
Enter the authorization code: 4/1AX4blahblahblah-I clicked the link above, authorized with my Youtube account, and pasted in the code it gave me here!
Traceback (most recent call last):
File "C:\Users\myUserName\PycharmProjects\MyProjectName\example_download_captions.py", line 54, in <module>
main()
File "C:\Users\myUserName\PycharmProjects\MyProjectName\example_download_captions.py", line 36, in main
credentials = flow.run_console()
File "C:\Users\myUserName\PycharmProjects\MyProjectName\venv\lib\site-packages\google_auth_oauthlib\flow.py", line 439, in run_console
self.fetch_token(code=code)
File "C:\Users\myUserName\PycharmProjects\MyProjectName\venv\lib\site-packages\google_auth_oauthlib\flow.py", line 298, in fetch_token
kwargs.setdefault("client_secret", self.client_config["client_secret"])
KeyError: 'client_secret'
我的代码非常基于从YouTube API参考页面上的YouTube API参考页面上生成的示例代码下载方法:
# https://developers.google.com/youtube/v3/docs/captions/download
# -*- coding: utf-8 -*-
# Sample Python code for youtube.captions.download
# NOTE: This sample code downloads a file and can't be executed via this
# interface. To test this sample, you must run it locally using your
# own API credentials.
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/code-samples#python
import io
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
from googleapiclient.http import MediaIoBaseDownload
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 = "C:\\Users\\myUserName\\Documents\\MyProjectDirectoryName\\client_secret_xxxxxxxx.apps.googleusercontent.com.json"
result_file = 'C:\\Users\\myUserName\\Documents\\MyProjectDirectoryName\\Video Transcripts\\vid_example_transcript.txt'
test_caption_id = 'VxPQ0cBI7BNjG-6Kpo8Wi1H6VXAziegGD0Be0NlMJCg='
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
print("flow_before")
credentials = flow.run_console()
print("flow_after")
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.captions().download(
id=test_caption_id
)
fh = io.FileIO(result_file, "wb")
download = MediaIoBaseDownload(fh, request)
complete = False
while not complete:
status, complete = download.next_chunk()
if __name__ == "__main__":
main()
JSON Secrets文件中的内容:
{"installed": {
"client_id": "abcd-1234.apps.googleusercontent.com",
"project_id": "MyProjectName",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"redirect_uris": [
"http://localhost"
]
}}
这是我获得此文件的方式:
i去 https://console.cloud.cloud。 google.com/apis/api/youtube.googleapis.com/credentials?project=myprojectName
在该页面上的“ OAuth 2.0客户端ID”下,我用与项目名称相同的名称制作的凭据,该类型为桌面。
在该行的最右边,凭据是一个动作列,我点击了下载图标,然后在弹出的框中,我点击了下载JSON按钮。
那么,有人知道我如何更改代码以使其正常工作吗?
I am trying to do a personal project related to gathering data from YouTube, and I am having a heck of a time using the API methods that require one to authenticate with OAuth2. The ones you can use an API key with, I'm all set on, and that part of my little app is working great. But downloading the transcript of a video requires OAuth2.
As a starting point to figuring OAuth2 out, I got example Python code generated from the Try It sidebar on the Youtube API Reference page for the Captions Download method. I modified it to include the caption ID for one of the videos I am interested in, the JSON secrets file for the project's OAuth 2.0 Client ID of type desktop that I downloaded from the Google Cloud Console, and the file path where I wanted it to put the transcript.
Unfortunately, it does not work! I've included below (with possibly-secret info replaced with placeholders) the code I was using, the contents of the secrets JSON file, and the contents of the console with the error message.
The code includes a call to flow.run_console()
which is marked in PyCharm as deprecated. When I hover my mouse over it for a couple seconds, this pops up above the documentation about the function:
New clients will be unable to use `InstalledAppFlow.run_console` starting on Feb 28, 2022. All clients will be unable to use this method starting on Oct 3, 2022. Use `InstalledAppFlow.run_local_server` instead. For details on the OOB flow deprecation, see https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html?m=1#disallowed-oob
At that site, I read about the security risks that made them deprecate this. Unfortunately, I could not figure out from that site how to replace it. Using credentials = flow.run_local_server()
instead of credentials = flow.run_console()
produced the exact same KeyError. It seems like the other options are all intended to be hosted on a website or used for mobile apps. I only ever want to use this app on my computer specifically.
It does seem to work as far as that line, or at least, it gives me a URL that lets me click, authorize my project to use my Youtube account, and paste the code back in. It's after I paste in the code that it fails. The error it gives me is actually about the credentials file not having a key with the name client_secret
-- which, indeed, is missing from my credentials file. Maybe they stopped including that in general and never updated the example-code-generator on the API reference? When searching for other answers, I see a lot of references to creating an OAuth2 ClientID with the type of "Installed / other" but that is no longer an option in the type dropdown, as far as I can see.
Or, maybe it's related to the fact that my app is still listed with Google as in "testing" mode? I don't intend to release it to the world, I'm the only one who will ever use this app. It's staying on my computer, so I don't have a website to give Google so that they can check out my privacy policy or whatever other requirements they need for a released app.
Error message when running code:
C:\Users\myUserName\PycharmProjects\MyProjectName\venv\Scripts\python.exe C:/Users/acrai/PycharmProjects/MyProjectName/example_download_captions.py
flow_before
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=xxxxxxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=urnxxxxxxoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.force-ssl&state=xxxxxxxxxx&prompt=consent&access_type=offline
Enter the authorization code: 4/1AX4blahblahblah-I clicked the link above, authorized with my Youtube account, and pasted in the code it gave me here!
Traceback (most recent call last):
File "C:\Users\myUserName\PycharmProjects\MyProjectName\example_download_captions.py", line 54, in <module>
main()
File "C:\Users\myUserName\PycharmProjects\MyProjectName\example_download_captions.py", line 36, in main
credentials = flow.run_console()
File "C:\Users\myUserName\PycharmProjects\MyProjectName\venv\lib\site-packages\google_auth_oauthlib\flow.py", line 439, in run_console
self.fetch_token(code=code)
File "C:\Users\myUserName\PycharmProjects\MyProjectName\venv\lib\site-packages\google_auth_oauthlib\flow.py", line 298, in fetch_token
kwargs.setdefault("client_secret", self.client_config["client_secret"])
KeyError: 'client_secret'
My code, very much based on the example code generated from the Try It sidebar on the Youtube API Reference page for the Captions Download method:
# https://developers.google.com/youtube/v3/docs/captions/download
# -*- coding: utf-8 -*-
# Sample Python code for youtube.captions.download
# NOTE: This sample code downloads a file and can't be executed via this
# interface. To test this sample, you must run it locally using your
# own API credentials.
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/code-samples#python
import io
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
from googleapiclient.http import MediaIoBaseDownload
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 = "C:\\Users\\myUserName\\Documents\\MyProjectDirectoryName\\client_secret_xxxxxxxx.apps.googleusercontent.com.json"
result_file = 'C:\\Users\\myUserName\\Documents\\MyProjectDirectoryName\\Video Transcripts\\vid_example_transcript.txt'
test_caption_id = 'VxPQ0cBI7BNjG-6Kpo8Wi1H6VXAziegGD0Be0NlMJCg='
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
print("flow_before")
credentials = flow.run_console()
print("flow_after")
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.captions().download(
id=test_caption_id
)
fh = io.FileIO(result_file, "wb")
download = MediaIoBaseDownload(fh, request)
complete = False
while not complete:
status, complete = download.next_chunk()
if __name__ == "__main__":
main()
What is in the JSON secrets file:
{"installed": {
"client_id": "abcd-1234.apps.googleusercontent.com",
"project_id": "MyProjectName",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"redirect_uris": [
"http://localhost"
]
}}
Here's how I obtained this file:
I went to https://console.cloud.google.com/apis/api/youtube.googleapis.com/credentials?project=MyProjectName
Under "OAuth 2.0 Client IDs" on that page is the credential I made with the same name as the project name, the type is Desktop.
At the far right of that line with the credential is an Actions column, I hit the Download icon, and then in the box that pops up, I hit the Download JSON button.
So, does anyone know how I can change my code to make it work?
发布评论