I am facing a huge problem using the youtube-api. I try to set a
custom thumbnail for one of my videos. Of course I have the permission
to set custom thumbnails for my videos and an authenticated youtube
service. I tried to do it in two ways (C#):
Method 1 using the .Net API:
[...]
Debug.WriteLine("Try to set thumbnail...");
thumbnailStream = File.OpenRead(@"D:\myThumbnail.jpg");
YouTubeService service = GetYouTubeService(); // Gets the authenticated youtube service
ThumbnailsResource tr = new ThumbnailsResource(service);
ThumbnailsResource.SetMediaUpload mediaUploadThumbnail = tr.Set(MY_VIDEO_ID, thumbnailStream, "image/jpeg");
mediaUploadThumbnail.ChunkSize = 256 * 1024;
mediaUploadThumbnail.ProgressChanged += mediaUploadThumbnail_ProgressChanged;
mediaUploadThumbnail.ResponseReceived += mediaUploadThumbnail_ResponseReceived;
Debug.WriteLine("... Starting async upload");
mediaUploadThumbnail.UploadAsync();
[...]
void mediaUploadThumbnail_ProgressChanged(IUploadProgress obj)
{
Debug.WriteLine("... Progress changed");
Debug.Write(" ... " + obj.Status.ToString() + ": " + obj.BytesSent + " bytes sent");
if (obj.Exception != null)
{
Debug.Write("... Exception:" + obj.Exception.ToString());
thumbnailStream.Close();
uploadList[aktuelleUploadPosition].Status = UploadVideoStatus.ThumbnailFehler;
objectListViewUploads.RefreshObjects(uploadList);
Starte_Uploads();
}
}
void mediaUploadThumbnail_ResponseReceived(ThumbnailSetResponse obj)
{
Debug.WriteLine("... Done: " + obj.Items[0].High.Url);
thumbnailStream.Close();
uploadList[aktuelleUploadPosition].Status = UploadVideoStatus.Fertig;
objectListViewUploads.RefreshObjects(uploadList);
Starte_Uploads();
}
The Output of Method 1 is:
Try to set thumbnail...
... Starting async upload
... Progress changed ... Starting: 0 bytes sent
... Progress changed ... Uploading: 262144 bytes sent
... Progress changed ... Uploading: 524288 bytes sent
... Done: https://i.ytimg.com/s_vi/CjiPNurZs2M/hqdefault.jpg?sqp=CJCH958F&rs=AOn4CLA5OGm9ZfythL5cri4cjV3CyP4Pmg
... Progress changed ... Completed: 750441 bytes sent
Method 2 using direct Webrequest:
Debug.WriteLine("Try to set thumbnail...");
RefreshToken(); // sets a new access token or refreshes the existing on
thumbnailStream = File.OpenRead(@"D:\myThumbnail.jpg");
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/upload/youtube/v3/thumbnails/set?videoId=" + MY_VIDEO_ID);
webReq.Method = "POST";
webReq.ContentLength = thumbnailStream.Length;
webReq.ContentType = "image/jpeg";
webReq.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + access_token);
byte[] thumbBuffer = ReadToEnd(thumbnailStream);
var reqStream = webReq.GetRequestStream();
reqStream.Write(thumbBuffer, 0, thumbBuffer.Length);
reqStream.Close();
try
{
HttpWebResponse resp = (HttpWebResponse)webReq.GetResponse();
byte[] bytes = new byte[(int)resp.ContentLength];
resp.GetResponseStream().Read(bytes, 0, (int)resp.ContentLength);
Debug.WriteLine("Content: " + System.Text.Encoding.UTF8.GetString(bytes));
}
catch (WebException ex)
{
byte[] bytes = new byte[(int)ex.Response.ContentLength];
ex.Response.GetResponseStream().Read(bytes, 0, (int)ex.Response.ContentLength);
Debug.WriteLine("Content: " + System.Text.Encoding.UTF8.GetString(bytes));
}
thumbnailStream.Close();
The Output of Method 2 is:
Try to set thumbnail...
Content: {
"kind": "youtube#thumbnailSetResponse",
"etag": "\"gMjDJfS6nsym0T-NKCXALC_u_rM/FktWbvFp7RSXY9xeTkZbzfsVtH0\"",
"items": [
{
"default": {
"url": "https://i.ytimg.com/s_vi/CjiPNurZs2M/default.jpg?sqp=CMCQ958F&rs=AOn4CLAr7zN45H1HExk4K_-y8ujYXWMBFw",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/s_vi/CjiPNurZs2M/mqdefault.jpg?sqp=CMCQ958F&rs=AOn4CLAJcATINZ2yuyNE1Ij6engYjZA5qw",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/s_vi/CjiPNurZs2M/hqdefault.jpg?sqp=CMCQ958F&rs=AOn4CLBOJfds9D6hyIFDyeYzr9oug52-Lg",
"width": 480,
"height": 360
}
}
]
}