2

I am need to upload video to youtube to specific youtube account. So I need to authenticate I am using java google lib. Code looks like this:

          Credential credential = new GoogleCredential.Builder()
            .setTransport(new ApacheHttpTransport())
            .setJsonFactory(new JacksonFactory())
            .setServiceAccountId("xxx@xx-app.iam.gserviceaccount.com")
            .setClientSecrets("xx@gmail.com", "xx")
            .setServiceAccountPrivateKeyFromP12File(new File("xx.p12"))
            .setServiceAccountScopes(scopes)
            .setServiceAccountUser("xx@gmail.com")
            .build();

          youtube = new YouTube.Builder(credential.getTransport(), credential.getJsonFactory(), credential).setApplicationName(
            "tellews-app").build();
          Video returnedVideo = videoInsert.execute();
          YouTube.Videos.Insert videoInsert = youtube.videos()
            .insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
          Video returnedVideo = videoInsert.execute();

And getting error:

IOException: 401 Unauthorized { "error" : "unauthorized_client", "error_description" : "Client is unauthorized to retrieve access tokens using this method." }

Maybe someone sees what I am doing wrong

1 Answers1

0

Simple.

Do not use a Service Account - that's not what they're there for.

You need to obtain an Access Token for the target YouTube account. The simples way to do that is to get yourself a Refresh Token for that account from the Oauth Playground, and use it to fetch an Access Token whenever you need one. The steps to do this are enumerated at How do I authorise an app (web or installed) without user intervention? (canonical ?) . In the comments there is a link to a YouTube video which also explains the steps.

Community
  • 1
  • 1
pinoyyid
  • 21,499
  • 14
  • 64
  • 115