YouTube API v3 Java授权 [英] YouTube API v3 Java authorization

查看:398
本文介绍了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.

您还需要从开发人员控制台将服务帐户的权限更改为是所有者。

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();

,如上面的网址所示。 emailAddress是服务帐户的电子邮件地址,必须更改P12文件名,集合.~~~应更改为范围(原始示例中的预制文件),最后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授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆