Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am trying to upload a video to YouTube by using YouTube data API, from my application but unfortunately the API which provided by YouTube developers
is not working before three weeks i tested the code and it was working fine but now when i try to finally use it in my application its causing a problem all the code is working fine but when the video uploading process starts it's became loading and loading and the video doesn't uploaded i google it but not find any help. if some one can help me ?

What I have tried:

i have tried this code
 private async Task Run()
        {
            UserCredential credential;
            using (var stream = new FileStream("G:\\client_secret_783534382593-0cn4gm229raqq97kdu0ghsj9hqfsc5o1.apps.googleusercontent.com.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows an application to upload files to the
                    // authenticated user's YouTube channel, but doesn't allow other types of access.
                    new[] { YouTubeService.Scope.YoutubeUpload },
                    "user",
                    CancellationToken.None
                );
            }

            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });

            var video = new Video();
            video.Snippet = new VideoSnippet();
            video.Snippet.Title = "ttry";
            video.Snippet.Description = " Video Description";
            video.Snippet.Tags = new string[] { "tag1s", "tag2s" };
            video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "public"; // or "private" or "publi
            var filePath = "F:\\Dna.MP4"; // Replace with path to actual movie file.

            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
                videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
                videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

                await videosInsertRequest.UploadAsync();
            }
        }

        void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
        {
            switch (progress.Status)
            {
                case UploadStatus.Uploading:
                    Console.WriteLine("{0} bytes sent.", progress.BytesSent);
                    break;

                case UploadStatus.Failed:
                    Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
                    break;
            }
        }

        void videosInsertRequest_ResponseReceived(Video video)
        {
            Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
        }
    }
}


and i called it simply from this action
public ActionResult Contact()
       {
           Run().Wait();

           return View();
       }

i wonder that why it is not working ? when i debug it then all the things and parameters are fine even credentials are also fine but at the end the file is not uploading even i am selecting 1 MB file and i also included all the necessary NuGet packages kindly some one help me please ?
Posted
Comments
Richard Deeming 22-Mar-16 14:53pm    
Suggestion: Rather than calling .Wait on your task, use an async action:

public async Task<ActionResult> Contact()
{
    await Run();
    return View();
}
Richard Deeming 22-Mar-16 14:54pm    
Also, Console.WriteLine isn't going to do anything in an ASP.NET application.
arslan afzal bhatti 22-Mar-16 15:47pm    
done but nothing happened! same problem it loads for a while and then go to contact view but the video is not uploading even i am trying to upload through console application but and there message shows that the video was successfully uploaded but when i checked it on youtube in my account there is no such video founded !
arslan afzal bhatti 22-Mar-16 15:48pm    
yep i know but for the sake of testing i just copy paste code and without any changing
FARONO 23-Mar-16 4:35am    
Did you maybe deploy it on another environment as the one you tested it on? I mean can you rule out firewall and security things?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900