alby_premia
alby_premia

Reputation: 61

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.

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

Upvotes: 6

Views: 2158

Answers (3)

vgonisanz
vgonisanz

Reputation: 11930

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.

Upvotes: 4

Vikas Raj
Vikas Raj

Reputation: 21

If you have created youtube account and if has been linked to your google account, then make sure you create your own channel in your youtube.

This will allow the API to upload videos to your created channel.

Hope it helps.

Upvotes: 2

Jeff Posnick
Jeff Posnick

Reputation: 56144

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.

Upvotes: 3

Related Questions