YouTube API v3 Java authorization

前端 未结 2 1463
有刺的猬
有刺的猬 2021-01-07 01:28

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 Cr

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-07 01:47

    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.

提交回复
热议问题