Max*_*eap 6 python youtube oauth youtube-api caption
我正在尝试使用YouTube Data API(v3)从YouTube视频中获取字幕 https://developers.google.com/youtube/v3/guides/implementation/captions
所以,首先我尝试使用此网址检索字幕列表:https://www.googleapis.com/youtube/v3/captions? part = snippet & videoId = KK9bwTlAvgo & key = {My API KEY}
我可以从上面的链接中检索我想要下载的标题ID(jEDP-pmNCIqoB8QGlXWQf4Rh3faalD_l).
然后,我按照此说明下载了标题:https: //developers.google.com/youtube/v3/docs/captions/download
但是,即使我正确输入了标题ID和我的api密钥,它也会显示"需要登录"错误.
我想我需要OAuth身份验证,但我想要做的是与用户的帐户无关,而只是自动下载公共字幕数据.
我的问题是:有没有办法只处理OAuth身份验证一次,以获取我自己的YouTube帐户的访问令牌,然后在我的应用程序中需要时重复使用它?
Tyl*_* C. 11
我不能特别谈到字幕API所需的权限,但总的来说,是的,您可以使用自己的帐户OAuth访问您的应用,并使用访问权限和刷新令牌向API发出后续的OAuth请求.您可以在此处找到生成令牌的详细信息:
https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps#Obtaining_Access_Tokens
手动执行这些步骤(幸运的是,您只需要执行一次):
要授予访问权限并接收临时代码,请在浏览器中输入以下URL:
https://accounts.google.com/o/oauth2/auth?
client_id=<client_id>&
redirect_uri=http://www.google.com&
scope=https://www.googleapis.com/auth/youtube.force-ssl&
response_type=code&
access_type=offline&
approval_prompt=force
Run Code Online (Sandbox Code Playgroud)
按照提示授予对应用的访问权限.
code参数重定向到google.com (例如
https://www.google.com/?code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&gws_rd=ssl#).保存代码.发送POST请求(例如,通过Postman Chrome插件)到https://accounts.google.com/o/oauth2/token,请求正文中包含以下内容:
code=<code>&
client_id=<client_id>&
client_secret=<client_secret>&
redirect_uri=http://www.google.com&
grant_type=authorization_code
Run Code Online (Sandbox Code Playgroud)然后,您可以使用访问令牌手动发送OAuth'd要求,下面的选项之一在这里,主要有:
curl -H "Authorization: Bearer ACCESS_TOKEN" https://www.googleapis.com/youtube/v3/captions/<id>
Run Code Online (Sandbox Code Playgroud)
要么
curl https://www.googleapis.com/youtube/v3/captions/<id>?access_token=ACCESS_TOKEN
Run Code Online (Sandbox Code Playgroud)
(但是当我尝试第二个字幕选项时,我收到消息:"查询字符串中收到了OAuth令牌,此API禁止使用JSON或XML以外的响应格式.如果可能,请尝试发送OAuth令牌相反,授权标题.")
您还可以在代码中使用刷新令牌来创建构建YouTube对象时所需的凭据.在Java中,这看起来如下所示:
String clientId = <your client ID>
String clientSecret = <your client secret>
String refreshToken = <refresh token>
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(jsonFactory)
.setClientSecrets(clientId, clientSecret)
.build()
.setRefreshToken(refreshToken);
try {
credential.refreshToken();
} catch (IOException e) {
e.printStackTrace();
}
youtube = new YouTube.Builder(transport, jsonFactory, credential).build();
Run Code Online (Sandbox Code Playgroud)
我想你可以使用API客户端库在Python中做类似的事情,尽管我还没有尝试过Python.
| 归档时间: |
|
| 查看次数: |
2399 次 |
| 最近记录: |