Reputation: 31
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.
Upvotes: 3
Views: 1005
Reputation: 115
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
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("[email protected]")
.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.
Upvotes: 2