YouTube API v3 Java authorization

余生长醉 提交于 2019-12-19 10:30:13

问题


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.

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 Any possible without invoking browser to upload video

If you have any idea you can help, I'd be really grateful. Thank you.


回答1:


I looked for ways to accomplish this and found it. I followed the instructions at https://developers.google.com/identity/protocols/OAuth2ServiceAccount

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.

Then change the code

Credential credential = Auth.authorize(scopes, "uploadvideo");

to

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

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.




回答2:


You could use SSH as a proxy and then use a local browser to use that proxy.

ssh -ND 8080 user@server.example.com That sets a SOCKS proxy. Just edit your browser's proxy settings to IP: localhost Port: 8080 and your traffic is going out over SSH.

Just remember that all your browser's traffic is going through the proxy until you disable it so don't do anything against network policy while you're logged in. I might suggest using a private instance of Chrome, for example.

Note: This probably won't work for domain lookups so you'll need the local (to the SSH host) IP of the router.



来源:https://stackoverflow.com/questions/31097266/youtube-api-v3-java-authorization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!