Youtube API V3 Java Any possible without invoking browser to upload video

对着背影说爱祢 提交于 2020-01-01 15:36:07

问题


Hi I hope someone can help me out here.

I have a Java Application on my local machine,I am trying to upload video to YouTube.

Upload a video to the authenticated user's channel. Use OAuth 2.0 to authorize the request.

It was working good.

The source code getting from Youtube API V3. The class name is com.google.api.services.samples.youtube.cmdline.data.UploadVideo

While I run the application everyday asking very first time invoking default browser once i click approve after that video upload to youtube. Second time not invoking default browser. It was working good.

But I want without invoking browser, Need to upload video to youtube.

Any Idea ? Please share me.


回答1:


I had the exact same problem as you did, and I figured it out. You can find the answer at YouTube API v3 Java authorization

Edit

Sorry, didn't realize link-only answers were discouraged. Just was so pleased that I solved the problem. Adding the details below :


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.



来源:https://stackoverflow.com/questions/30410410/youtube-api-v3-java-any-possible-without-invoking-browser-to-upload-video

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