YouTube API v3 Java authorization(YouTube API v3 Java 授权)
问题描述
我正在使用 YouTube 数据 API (Java) 将视频上传到我的 YouTube 频道.我已经在我的 Windows PC 上对其进行了测试并成功了.但是示例中的授权通过打开浏览器窗口来登录 Google 来创建 Credential 实例.这在我的 Windows PC 上很好,但我试图让这段代码在我只有 SSH 访问权限的远程 linux 机器上工作.
I'm using the YouTube Data API (Java) to upload videos to my YouTube channel. I've tested it on my Windows PC and succeeded. But the authorization in the sample makes a Credential instance by opening a browser window to signin to Google. This is fine on my Windows PC, but I'm trying to get this code to work on a remote linux machine that I only have SSH access to.
我在 stackoverflow 上搜索了类似的问题,并找到了完全相同的问题.但由于该问题没有 Google 工程师需要的特定标签,因此我将其作为新问题发布.
I've searched stackoverflow for similar questions, and found the exact same question. But as that question doesn't have the specific tags that Google engineers require, I'm posting it as a new question.
Youtube API V3 Java 任意无需调用浏览器即可上传视频
如果您有任何想法可以提供帮助,我将不胜感激.谢谢.
If you have any idea you can help, I'd be really grateful. Thank you.
推荐答案
我想方设法做到这一点并找到了.我按照 https://developers.google.com/identity/protocols/OAuth2ServiceAccount
I looked for ways to accomplish this and found it. I followed the instructions at https://developers.google.com/identity/protocols/OAuth2ServiceAccount
您需要一个新的 OAuth 客户端 ID 并将其设置为开发者控制台中的服务帐户" - API 和auth - 凭据,然后下载 P12 密钥.
You need a new OAuth Client ID and set it up as an "Service account" in the Developers Console - APIs & auth - Credentials, and then download the P12 key.
您还需要在 Developers Console 中将服务帐户的权限更改为是所有者".
You also need to change the Permissions of the service account to "Is owner" from the Developers Console.
然后改代码
Credential credential = Auth.authorize(scopes, "uploadvideo");
到
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountPrivateKeyFromP12File(new File("MyProject.p12"))
.setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))
.setServiceAccountUser("user@example.com")
.build();
如上面的 URL 中指定的那样.emailAddress 是来自服务帐户的电子邮件地址,P12 文件名必须更改,Collections.~~~ 应更改为范围(原始示例中的预制),最后 serviceAccountUser 应为您的原始 Gmail ID.
as specified in the URL above. emailAddress is the email address from the service account, the P12 filename must be changed, Collections.~~~ should be changed to scopes (the premade one in the original example), finally the serviceAccountUser should be your original Gmail ID.
上面的方法我成功了,希望对你有帮助.
I succeeded with the above method, hope it helps.
这篇关于YouTube API v3 Java 授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:YouTube API v3 Java 授权


- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01