Siobhan Power Siobhan Power - 28 days ago 21
C# Question

Youtube api upload video.net

Im trying to create a website from which you can upload videos from your device to youtube but be able to view those specific videos you uploaded form my site.(there's more purpose to it aswell)

Im using .net c# and I have my oauth keys and developer api keys, downloaded the json file etc. I downloaded the sample code google has for this but cant seem to get it working, not fully sure how it does work.

Could someone please help me out with this / explain it to me?

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;


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;

/// <summary>
/// Summary description for Video_Upload
/// </summary>
public class Video_Upload
{
public Video_Upload()
{
//
// TODO: Add constructor logic here

new Video_Upload().Run().Wait();

}

private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("Client_id_googleApi.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
});

//VIDEO INFO AND DETAILS

var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = "Test Video 1";
video.Snippet.Description = "Testing Video Upload";
video.Snippet.Tags = new string[] { "Test", "First" };
video.Snippet.CategoryId = "17";//category id for sport // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Snippet.ChannelId = "UCfvR-wqeoHmAGrHnoQRfs9w";
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "public"; // or "private" or "public"
var filePath = @"C:\Users\siobhan\Documents\Visual Studio 2015\WebSites\FYP_November\GP010149.avi"; // 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(progress.BytesSent, " bytes sent.");
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);
}


}



}

Answer

It says 'void cannot be used in this way'

Because you're trying to declare a method inside of another method:

private async Task Run()
{
    //...

    void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
    {
        //...
    }

    //...
}

This isn't valid in C#. Methods are declared inside of a class and are essentially alongside each other. Such as:

private async Task Run()
{
    //...
}

void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
    //...
}
Comments