0
votes

I want my users to be able to upload videos to MY YouTube channel under MY YouTube account. Hence I don't really want or need them to authenticate (OAuth) as themselves.

I've seen the examples here: https://developers.google.com/youtube/v3/code_samples/dotnet

All of the code examples I find seem to use OAuth and client secrets to authenticate. Can anybody point me in the direction of a nice tutorial/sample whereby I can upload a video to MY Channel as MYself (using an API key?)?

Thanks.

UPDATE

Well....I'm not even sure if what I've done is right....but here is what I've done:

Went to Credentials for my project: https://console.developers.google.com/apis/credentials

Created 2 OAuth credenials - one was Web Application and one was Other. I downloaded the JSON for these and added them to my project so I could test each one.

I enabled the YouTube data API here: https://console.developers.google.com/apis/api/youtube.googleapis.com/overview

I largely took the .Net upload example from here: https://developers.google.com/youtube/v3/code_samples/dotnet

I also installed the Nuget package for the YouTube API v3

before "var youtubeService =..." I added this:

// This bit checks if the token is out of date, 
        // and refreshes the access token using the refresh token.
        if (credential.Token.IsExpired(SystemClock.Default))
        {
            if (!await credential.RefreshTokenAsync(CancellationToken.None))
            {
                Console.WriteLine("No valid refresh token.");
            }
        }       

also created the following class - again, I obtained it from another StackOverflow post - with the aim of creating an offline access token??

 public class OfflineAccessGoogleAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
{
    public OfflineAccessGoogleAuthorizationCodeFlow(GoogleAuthorizationCodeFlow.Initializer initializer) : base(initializer) { }

    public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri)
    {
        return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl))
        {
            ClientId = ClientSecrets.ClientId,
            Scope = string.Join(" ", Scopes),
            RedirectUri = redirectUri,
            AccessType = "offline",
            ApprovalPrompt = "force"
        };
    }
}

So I've tested it, and the upload eventually 'seems' to work, however:

  1. The code never returns back to the View from await videosInsertRequest.UploadAsync(); and just seems to hang....UPDATE - Fixed. See here: https://www.codeproject.com/Questions/1087360/Youtube-data-API-for-uploading-videos-not-working

  2. The video never actually appears in my channel - i have a feeling YouTube ban it before it's even uploaded? (it's just a test video - nothing sinister - could that be why?)

  3. I have no idea if I am still doing this right.

Any more advice please? I still struggle to believe nobody has a working example of what I'm after. I can't believe that a user....uploading a video to my web server.....and then uploading to my own channel is so difficult!!

2
Let the users send the videos to your web application and then from there upload it to your account using oauth.Gusman
Would OAuth require me to manually authenticate? I don't want any manual intervention from my side to upload this to YouTube....Captain_Planet
I'm not sure how that link helps? I have already created an API key and an OAuth ID. I'm literally wanting to upload videos uploaded by my users to my own channel - hence I don't understand why I'd need to use OAuth and not the API key instead? Are there no examples of using the API key instead?Captain_Planet
I obtained a nice piece of code from here: stackoverflow.com/a/27691403/4181058 But i think the method of getting a long-term refresh key has changed. 'Installed Application' no longer seems to exist....Captain_Planet

2 Answers

0
votes

First off: It is impossible to upload videos with API keys.

From a technical point of view, API keys are not tied to a specific YouTube channel, but to a Google Cloud Console project. That means even if YouTube allowed API keys as authentication for uploading videos, they wouldn't know which channel to upload to.

For all write actions (e.g. uploading videos), proper authentication via OAuth 2.0 is required. Therefore, there are no tutorials that show how to upload using an API key.

As @Gusman said, you would need to let your users upload to your server, and then your server uploads to YouTube using OAuth credentials.

Regarding "'Installed Application' no longer seems to exist...":

The way you work with API keys and OAuth client IDs/secrets in your code has stayed exactly the same. Google just changed how you create and restrict access to keys in the Cloud Console.

I encourage you to familiarize yourself with the concept of server-side apps that use the YouTube API.

EDIT: to clarify the workflow

  1. You set up a Cloud Console project and an OAuth client ID.
  2. You give your permission to your application that it may upload videos to your channel. Your application will receive tokens (access token and refresh token) as a result.
  3. Users of your application upload their videos to your application server.
  4. Your application takes the user's video (which is now locally on your server) and the tokens from step 2 and uploads the video to YouTube with those tokens.
0
votes

You must use oAuth, but what you want to skip is the user authentication step. See this SO answer here

You need to obtain a refresh token and store that in your code along with the OAuth Key and Secret.

As for your "banned" video. If the video was uploaded as 'unlisted' it will not show up until you navigate to your Creator Studio -> Videos list