Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoLinkedYoutubeAccount error 401 when uploading videos on Youtube with java

i would like to upload videos on youtube from my java web application: I want to receive (server side) my authToken and form-action so to upload a video.

  • I've created a new google/youtube account
  • I've created a youtube channel in my account
  • i've created a new project in my google console api as a Service Account

Now i'm trying to implement my code with oAuth 2.0. I've got the accessToken but, when i try to call the service getFormUploadToken(url, object) the response is always the same "NoLinkedYoutubeAccount error 401".

i've also verified the account by the google support page http://www.youtube.com/my_account_unlink but it's seems ok.

Does anybody have an idea about this problem?

This is my code:

    HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    JsonFactory JSON_FACTORY = new JacksonFactory();
    String accessToken = "";
    GoogleCredential credential = null;


    credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(CLIENT_EMAIL)
            .setServiceAccountScopes("http://gdata.youtube.com")
            .setServiceAccountPrivateKeyFromP12File(new File(PRIVATE_KEY_PATH))
            .build();
    credential.refreshToken();
    accessToken = credential.getAccessToken();


    YouTubeService service = new YouTubeService(CLIENT_ID, DEV_KEY);
    service.setAuthSubToken(accessToken, null);
    VideoEntry newEntry = new YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
    mg.setTitle(new MediaTitle());
    mg.getTitle().setPlainTextContent("My Test Movie");

    URL uploadUrl = new URL("http://gdata.youtube.com/action/GetUploadToken");
    FormUploadToken token = service.getFormUploadToken(uploadUrl, newEntry);

Thanks a lot, Albert III

like image 395
alby_premia Avatar asked Dec 05 '12 14:12

alby_premia


2 Answers

As the error say, you have not linked the account credential to youtube.

You can solve that problem: - Connecting with the account used on the app in www.youtube.com - Trying to upload a video - Setting name and surname when youtube ask, and process to link to gmail.

Done!!! I tried before using my app to upload the video using Firefox.

like image 84
vgonisanz Avatar answered Oct 16 '22 04:10

vgonisanz


You can't associate a YouTube channel with a Service Account, and you're trying to upload under the context of the Service Account.

Instead of using Service Accounts, you should go through the OAuth 2 flow and authorize access while logged in to the browser using the account that you actually want to upload into.

I understand the benefits of using Service Accounts, but YouTube is not set up to work with them at this time.

like image 44
Jeff Posnick Avatar answered Oct 16 '22 06:10

Jeff Posnick