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.

相关文章