1

I have been using google's reputed API V2 for uploading the videos on YouTube from my web application as there has been up gradation in API to V3 , I am unable to upload the same through the same code.

I have tried with the new application for uploading the video on YouTube with V3, but there are many things which were there before but not available in V3

OLD: From: Google.Gdata.YouTube.Youtubeservice

YouTubeService service = new YouTubeService(applicationName,developerKey);
service.setUserCredentials(googleEmail, googleEmailPassword);

NEW: From : Google.Apis.Youtube.V3.YoutubeService

YouTubeService service = new YouTubeService();` -- its doesn't have the set credentials

I have gone through YouTube Data API .NET Code Samples, but it was not much useful as it’s an console application.

1 Answer 1

1

You cant use Login and password anymore. You need to use Oauth2 to authenticate a user. The code is the same if it is a web or a console application.

/// <summary>
/// Authenticate to Google Using Oauth2
/// Documentation https://developers.google.com/accounts/docs/OAuth2
/// </summary>
/// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
/// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
/// <param name="userName">A string used to identify a user.</param>
/// <returns></returns>
public static YouTubeService AuthenticateOauth(string clientId, string clientSecret, string userName)
        {

            string[] scopes = new string[] { YouTubeService.Scope.Youtube,  // view and manage your YouTube account
                                             YouTubeService.Scope.YoutubeForceSsl,
                                             YouTubeService.Scope.Youtubepartner,
                                             YouTubeService.Scope.YoutubepartnerChannelAudit,
                                             YouTubeService.Scope.YoutubeReadonly,
                                             YouTubeService.Scope.YoutubeUpload}; 

            try
            {
                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , userName
                                                                                             , CancellationToken.None
                                                                                             , new FileDataStore("Daimto.YouTube.Auth.Store")).Result;

                YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "YouTube Data API Sample",
                });
                return service;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                return null;

            }

        }    }

Usage:

 // Authenticate Oauth2
    String CLIENT_ID = "xxxxx-d0vpdthl4ms0soutcrpe036ckqn7rfpn.apps.googleusercontent.com";
    String CLIENT_SECRET = "NDmluNfTgUk6wgmy7cFo64RV";
    var service = Authentication.AuthenticateOauth(CLIENT_ID, CLIENT_SECRET, "test");

code ripped from the Google-dotnet-Samples/ youtube

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Dalm, Thanks for your precious time and reply. It helped me but its redirecting to gmail userID and Password screen, also I am unable to work on my local system as its giving '400. That’s an error. Error: redirect_uri_mismatch Application: UplaodTo YouTube' which i think it needs https only and my local url is some what Localhost/ if there is any suggestions on his please do the needful. Thanks in advance.
The location you are sending from must match exactly what you put in the redirect uri field in Google Devloper console. It must be the exact file you would navigate to on your machine localhost/youtube.aspx for example
Hi Dalm, Thanks for that but this is asking for login credentials every time if its not "logged in" in browser , i want some method which will not ask for the gmail credentials in a popup when i am uploading a video, i want to handle it from the server side code itself as it was there in previous authorization version method 'service.setUserCredentials(googleEmail, googleEmailPassword);' which will return the service or something like token it should return, if you have any snippet of code for this let me know.
Client login was shut down you cant use login and password. You must use oauth2 and authenticate. There is no other way, there is no hack, there is no workaround. Google shut down the Authentication endpoint that allowed for login and password. You must use this method Oauth2 is the only way to access the YouTube API.
Hi Dalm, thanks for the reply, but if i use the above code its asking for the username and password in separate popup whenever i try to upload the video in my application as user will not know the email and password (it will be security violation) so please let me know where there is some method where i can validate the user at server side and upload video directly to my playlist in YouTube. even though the i have used Client ID and Client Secret ID of Oauth2 its asking the same. Please help me out if possible.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.