Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Youtube Video Upload - OAuth2 authorization Error - unauthorized_client

$
0
0

I am trying to upload a youtube video from a .NET C# windows console app. I get an exception during the Google.Apis.Upload.IUploadProgress progress:

Exception {"Error:"unauthorized_client", Description:"Unauthorized", Uri:"""} System.Exception {Google.Apis.Auth.OAuth2.Responses.TokenResponseException}

My code:

//...using System.IO;using Google.Apis.Auth.OAuth2;using Google.Apis.Services;using Google.Apis.Upload;using Google.Apis.Util.Store;using Google.Apis.YouTube.v3;using Google.Apis.YouTube.v3.Data;//...private void btnTest_Click(object sender, EventArgs e){    if (Path.GetFileName((@"D:\testdata\test.avi")) != "")    {        string vID = "none";        string sfilename = @"D:\testdata\test.avi";        string svideoName = "My Video Name";        string svideoDesc = "My Video Description";        string[] stags = new String[2];        stags[0] = "my tag 1";        stags[1] = "my tag 2";        string sSecret = "My Client Secret";        //**Obtained From https://console.cloud.google.com/apis/credentials**        string sClient_ID = "My Client ID";        //**Obtained From https://console.cloud.google.com/apis/credentials**        string sRefresh_Token = "My Refresh Token";        // **Obtained From OAuth 2.0 Playground**,         // **For scope I used "https://www.googleapis.com/auth/youtube.upload"**        FileStream fs = new FileStream(sfilename, FileMode.Open);        YouTubeUtilities ytU = new YouTubeUtilities(sRefresh_Token, sSecret, sClient_ID);        {            vID = ytU.UploadVideo(fs, svideoName, svideoDesc, stags, "22", false);        }        //Response.Write(vID);    }}public class YouTubeUtilities{    private String CLIENT_ID { get; set; }    private String CLIENT_SECRET { get; set; }    private String REFRESH_TOKEN { get; set; }    private String UploadedVideoId { get; set; }    private YouTubeService youtube;    public YouTubeUtilities(String refresh_token, String client_secret, String client_id)    {        CLIENT_ID = client_id;        CLIENT_SECRET = client_secret;        REFRESH_TOKEN = refresh_token;        youtube = BuildService();    }    private YouTubeService BuildService()     {        ClientSecrets secrets = new ClientSecrets()        {            ClientId = CLIENT_ID,            ClientSecret = CLIENT_SECRET        };        var token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse { RefreshToken = REFRESH_TOKEN };        var credentials = new UserCredential(new Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow(            new Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow.Initializer            {                ClientSecrets = secrets            }),"My Email Address", // Used to log in to google APIs and services            token);        var service = new YouTubeService(new BaseClientService.Initializer()        {            HttpClientInitializer = credentials,            ApplicationName = "My Project Name" // The name of my OAuth 2.0 client        });        //service.HttpClient.Timeout = TimeSpan.FromSeconds(360); // Choose a timeout to your liking        return service;    }    public String UploadVideo(Stream stream, String title, String desc, String[] tags, String categoryId, Boolean isPublic)    {        var video = new Video();        video.Snippet = new VideoSnippet();        video.Snippet.Title = title;        video.Snippet.Description = desc;        video.Snippet.Tags = tags;        video.Snippet.CategoryId = categoryId; // See https://developers.google.com/youtube/v3/docs/videoCategories/list        video.Status = new VideoStatus();        video.Status.PrivacyStatus = isPublic ? "public" : "unlisted"; // "private" or "public" or unlisted        //var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", stream, "video/*");        var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", stream, "video/*");        videosInsertRequest.ProgressChanged += insertRequest_ProgressChanged;        videosInsertRequest.ResponseReceived += insertRequest_ResponseReceived;        videosInsertRequest.Upload();        return UploadedVideoId;    }    public void DeleteVideo(String videoId)    {        var videoDeleteRequest = youtube.Videos.Delete(videoId);        videoDeleteRequest.Execute();    }    void insertRequest_ResponseReceived(Video video)    {        UploadedVideoId = video.Id;        // video.ID gives you the ID of the Youtube video.        // you can access the video from        // http://www.youtube.com/watch?v={video.ID}    }    void insertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)    {        // You can handle several status messages here.        switch (progress.Status)        {            case UploadStatus.Failed:                UploadedVideoId = "FAILED";                break;            case UploadStatus.Completed:                break;            default:                break;        }    }}

I am using code that I found online here:Upload video to youtube with mvc application (all code behind)

and other various posts - but could not get it working.


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>