我无法说出特定字幕API所需的权限,但通常情况下,您可以使用自己的帐号OAuth到您的应用程序,并使用访问和刷新令牌进行后续OAuth'd请求API。你可以在这里找到令牌生成的细节:
https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps#Obtaining_Access_Tokens
要手动执行这些步骤(幸运的是,你只需做一次):
- 如果访问已经被授予了应用程序,它需要被删除,以便建立新的认证凭证。转到https://security.google.com/settings/security/permissions(登录到您的帐户)并删除对应用的访问权限。如果客户端ID或秘密更改(或者您需要创建一个),请在API Manager的https://console.developers.google.com处找到它们。
授予访问权限,并获得临时代码,在浏览器中输入这个网址:
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
按照提示授予访问应用程序。
- 这将通过
code
参数(例如, https://www.google.com/?code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&gws_rd=ssl#)重定向到google.com。保存代码。
发送POST请求(例如,经由Postman Chrome plugin)至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
- 响应将包含访问令牌和刷新令牌。保存两者,但特别是刷新令牌(因为访问令牌将在1小时内过期)。
然后,您可以使用访问令牌手动发送OAuth'd要求,下列选项here之一,主要有:
curl -H "Authorization: Bearer ACCESS_TOKEN" https://www.googleapis.com/youtube/v3/captions/<id>
或
curl https://www.googleapis.com/youtube/v3/captions/<id>?access_token=ACCESS_TOKEN
(当我试图然而,第二个字幕选项是,我收到了以下消息:“OAuth令牌在查询字符串中被接收,该API禁止除JSON或XML之外的响应格式。如果可能,请尝试在Autho中发送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();
我想象你能做到在Python与API Client Libraries类似的东西,虽然我还没有尝试过的Python。
我也有这个问题。这让我非常恼火,因为API可以让你通过Key去做几乎所有的事情,但是自发地期望用户也可以加载OAuth2信誉,仅仅是因为他们可怕的机器翻译因为某些原因需要你的驾驶执照号码,护照,最近的3个电话账单,证明您的地址,高分辨率的照片。顺便说一下,API页面似乎没有提到任何地方这个提升的安全要求...... – 1owk3y