Reputation: 73
I'm trying to upload a video in youtube with C# Win application with the code below:
public Form1()
{
InitializeComponent();
Console.WriteLine("YouTube Data API: Upload Video");
Console.WriteLine("==============================");
try
{
new UploadVideo().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
//Console.WriteLine("Error: " + e.Message);
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
This is the class UploadVideo:
internal class UploadVideo
{
public async Task Run()
{
UserCredential credential;
using (var stream = new FileStream(@"C:\Users\23679\Downloads\client_secret.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
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 = "Default Video Title";
video.Snippet.Description = "Default Video Description";
video.Snippet.Tags = new string[] { "tag1", "tag2" };
video.Snippet.CategoryId = "22";
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "private";
var filePath = @"C:\Users\23679\Downloads\spacetestSMALL.wmv";
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);
}
It runs ok, then a browser window open asking permition like below:
The problem is that he return this message in the browser after i confirm:
So, I have two questions. What this message means?
And the second is, After that, what should happened? Because, the video is not uploaded and the debug don't continue...
Upvotes: 2
Views: 969
Reputation: 1105
I am not familiar with C# but I have some basic knowledge of OAuth 2.0 Authorization Code grant. I have made a web-sequence diagram which can help you.
On the first screenshot you shared the URI contains a redirect_uri query parameter with your callback url. This request gets a response with HTTP 302 redirect to the callback uri with the code=...
query parameter. You application should handle this request and exchange this code
to an access_token
.
My assumption that you can find C# libraries which helps you to handle these redirections and callas in order to receive an access_token
and also a refresh_token
, like in the RFC:
Response from OAuth 2.0 compliant server:
HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=SplxlOBeZQQ&state=xyz
Local application should forge this request:
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=SplxlOBeZQQ
&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
Response from the OAuth 2.0 compliant server:
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token":"2YotnFZFEjr1zCsicMWpAA",
"token_type":"example",
"expires_in":3600,
"refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
"example_parameter":"example_value"
}
This websequence diagram I made could be a good explanation.
Upvotes: 3