Youtube now has a Live Streaming section that allows users to broadcast their own live stream sessions. In this "Live Streaming" section, there are 2 options: "Live Now [Beta]" and "Events".
Live Now is a fast and easy way to start a streaming session automatically just by pointing your video encoder to te specified RTMP Url and Stream Key. It will automatically detect incomming media and start broadcasting publicly.
Events is pretty much the same thing, but with advance settings, although it will not start automatically to broadcast, and you need to set everything pretty much manually.
I know Youtube API allows you to retrieve Event's ingestion url and streamkey, so you can broadcast to that target, but it also requires to manage many other steps manually (like publishing the stream, binding broadcasts with streams, check the status, start, stop, etc..). On the other hand "Live Now" makes everything automatically.
Question: How can I retrieve "Live Now" ingestion info (rtmp url and streamkey) from the Youtube API v3 ?
The default broadcast can be retrieved by livebroadcasts.list with broadcastType set to "persistent".
The default livestream can be retrieved by livestreams.list using boundstreamid.
You cannot retrieve "Live Now" ingestion info because the API does not differentiate between "Live Now" and "Events." Those two options are provided as interfaces on top of the API for an end user, so they don't have to write their own application that interfaces with the API.
You will have to manually set up liveBroadcast and liveStream objects, bind them with liveBroadcasts.bind, test your stream, and transition to live on the liveStream object using status.streamStatus.
To Get “Live Now” rtmp and streamkey
$broadcastsResponse = $youtube->liveBroadcasts->listLiveBroadcasts(
'id,snippet,contentDetails',
array(
'broadcastType' => 'persistent',
'mine' => 'true',
));
$boundStreamId = $broadcastsResponse['items']['0']['contentDetails']['boundStreamId'];
$streamsResponse = $youtube->liveStreams->listLiveStreams('id,snippet,cdn', array(
// 'mine' => 'true',
'id' => $boundStreamId
));
print_r($streamsResponse);
// Keep client_id,client_secret and redirect_uri the client_secrets.json
UserCredential credential;
string BoundStreamId = string.Empty;
string StreamKey=string.Empty;
using (var stream = new FileStream("client_secrets.json", FileMode.Open,
FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.Youtube,YouTubeService.Scope.YoutubeReadonly},
"user",
CancellationToken.None,
new FileDataStore(this.GetType().ToString())
);
}
if (credential != null)
{
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "MyApp" // your app name
});
LiveBroadcastsResource.ListRequest lbRequest = youtubeService.LiveBroadcasts.List("id,snippet,contentDetails,status");
lbRequest.BroadcastType = LiveBroadcastsResource.ListRequest.BroadcastTypeEnum.Persistent;
lbRequest.MaxResults = 10;
lbRequest.Mine = true;
var bcResponse = await lbRequest.ExecuteAsync();
IEnumerator<LiveBroadcast> iLB = bcResponse.Items.GetEnumerator();
while (iLB.MoveNext() && string.IsNullOrEmpty(liveChatId))
{
BoundStreamId = livebroadcast.ContentDetails.BoundStreamId;
}
LiveStreamsResource.ListRequest lsRequest =
youtubeService.LiveStreams.List("id,snippet,cdn,status");
lsRequest.MaxResults = 50;
lsRequest.Id = BoundStreamId;
var srResponse = await lsRequest.ExecuteAsync();
IEnumerator<LiveStream> iLS = srResponse.Items.GetEnumerator();
if (srResponse != null)
{
foreach(LiveStream lvStream in srResponse.Items)
{
StreamKey= lvStream.Cdn.IngestionInfo.StreamName);
}
}
}
Related
We are using the ms graph api to post messages to a teams channel from a internal desktop application. The main purpose is to attach images to the message. We upload the image files into the one-drive folder of the channel as shown below.
var uploadProps = new DriveItemUploadableProperties
{
ODataType = null,
AdditionalData = new Dictionary<string, object>
{
{ "#microsoft.graph.conflictBehavior", "replace" }
}
};
var session = await graphClient.Drives[driveId]
.Items[parentId].ItemWithPath(fileName).CreateUploadSession(uploadProps).Request().PostAsync(token);
int maxSliceSize = 320 * 1024;
var fileUploadTask =
new LargeFileUploadTask<DriveItem>(session, fileStream, maxSliceSize);
// Create a callback that is invoked after each slice is uploaded
IProgress<long> progress = new Progress<long>(reportAsync);
// Upload the file
var uploadResult = await fileUploadTask.UploadAsync(progress);
if (uploadResult.UploadSucceeded)
{
return uploadResult.ItemResponse;
}
We then send a message to the channel and attach the images uploaded previously as reference attachments.
var chatMsg = new ChatMessage();
chatMsg.Body = new ItemBody();
chatMsg.Body.ContentType = BodyType.Html;
chatMsg.Body.Content = msg + " " + string.Join(" ", attachments.Select(d => $"<attachment id=\"{parseEtag(d.ETag)}\"></attachment>"));
chatMsg.Attachments = attachments.Select(d => new ChatMessageAttachment()
{
Id = parseEtag(d.ETag),
ContentType = "reference",
ContentUrl = d.WebUrl,
Name = d.Name
});
return await this.graphClient.Teams[teamId].Channels[channelId].Messages
.Request()
.AddAsync(chatMsg, token);
The problem is that the message only shows the names of the attachments with no preview as seen in the message at the bottom. We want to have a preview as seen (top message) when attaching a file within the teams application.
We've tried to set the thumbnailurl property of the attachment to the thumbnail url fetched from the ms-graph api with no success.
We've uploaded a file using the teams application (with preview) and then created an identical message with the same file (same driveitem id) in our application (show's no preview). Then we fetched both messages using the graph api and could not discern any differences between the two besides the message id's ofc.
We've scoured these forums, the ms documentations and even suggestion pages and found nothing.
We have been able to show previews separately in the body of the message referencing the thumbnail urls and in messagecards but ideally we want the preview directly in the attachments.
EDIT
The thumbnail urls seem to expire after 24 hours and are therefor not a great solution.
We managed to solve exactly this problem using the Simple Upload Api, with the added ?$expand=thumbnails query parameter. I haven't tried but the query param ought to work for the endpoint you're using as well.
Pick a size from the ThumbnailSet in the upload response and add it to the body of your message as an image tag. See below:
// channel, file, extractIdFromEtag, message omitted for brevity.
// PUT /groups/{group-id}/drive/items/{parent-id}:/{filename}:/content
const uploadUrl = `https://graph.microsoft.com/beta/groups/${channel.teamId}/drive/items/root:/${channel.displayName}/${file.name}:/content?$expand=thumbnails`;
const res = await this.http.put(uploadUrl, file).toPromise(); // FYI Using Angular http service
const attachment = {
id: extractIdFromEtag(res.eTag),
contentType: 'reference',
contentUrl: res.webUrl,
name: res.name,
thumbnailUrl: res.webUrl
};
const postBody = {
subject: null,
body: {
contentType: 'html',
content: message
},
};
// This is what makes the image show in the message as if posted from teams
postBody.body.content += `<br><br><img src="${res.thumbnails[0].large.url}" alt="${res.name}"/>`;
const messageUrl = `https://graph.microsoft.com/beta/teams/${channel.teamId}/channels/${channel.id}/messages`;
const result = await this.http.post(messageUrl, postBody).toPromise();
// Done
You can also keep adding the attachment as you already do, if you want the original image attached as a file, as well as showing the image preview in the message.
I've been looking at the MusicKit functionality for playlists:
https://developer.apple.com/documentation/applemusicapi/create_a_new_library_playlist
I'm wondering, can anyone confirm if they have been able to:
remove songs from an existing playlist
delete a playlist
update the title of a playlist
For example, I have tried updating the title of a playlist in c# using the following but the endpoint does exist/accept this. Note the appended playlist ID to the POST URL p.ABC123
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + [MYDEVTOKEN]);
client.DefaultRequestHeaders.Add("Music-User-Token", [MYMUSICUSERTOKEN]);
string _postUri = "https://api.music.apple.com/v1/me/library/playlists/p.ABC123";
var jsonObject = JObject.FromObject(new
{
attributes = new
{
name = "Playlist - Edited Title",
description = "This is a playlist edit"
}
});
var _content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
var response = await client.PostAsync(_postUri, content: _content);
string outputContent = await response.Content.ReadAsStringAsync();
}
It seems as though Apple isn't allowing this functionality.
https://forums.developer.apple.com/thread/107807
They could be doing this as a security precaution. However, Apple doesn't have a great relationship with the developer community, and is most likely doing it to limit people from building applications on top of theirs. (even though they are an extremely expensive API to work with off the bat...)
I wouldn't anticipate getting this functionality any time soon :(
For an End Screens report, how can I determine which target is represented by the end_screen_element_id on each record?
For example: on my channel, let's say I have "Video 1" set up with two video end screen elements "Video 2" and Video 3". I want to know how many clicks at the end of "Video 1" went to "Video 2" and how many went to "Video 3".
The data returned for this report gives me a video_id field indicating which video was watched and an end_screen_element_clicks field indicating how many times a viewer clicked through to the end screen video...
...HOWEVER, the only identifier for which video the viewer clicked through to is an end_screen_element_id field, which looks like a GUID and apparently somehow refers to the full end screen element definition, and therefore presumably what video is represented by that definition.
I'm unable to find any reports or other API calls for getting detail information on that end_screen_element_id field, particularly which video it represents.
How can I use that field or otherwise figure out what end screen video the viewer clicked through to?
More Information
The data returned looks like this:
Data returned in the End Screens report
Here's a screenshot that may help explain what I'm trying to do with the data: YouTube Analytics screen shot
The following C# code demonstrates how the report is requested:
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
{
ClientId = CLIENT_ID,
ClientSecret = CLIENT_SECRET
},
new[] { YouTubeReportingService.Scope.YtAnalyticsReadonly },
"user",
CancellationToken.None,
new FileDataStore("Drive.Auth.Store")).Result;
YouTubeReportingService reportingService = new YouTubeReportingService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = APPLICATION_NAME
});
// Submit a report job to obtain the latest End Screen statistics.
Job channelEndScreenJob = new Job();
channelEndScreenJob.ReportTypeId = "channel_end_screens_a1";
channelEndScreenJob.Name = JOB_NAME;
Job createdJob =
reportingService.Jobs.Create(channelEndScreenJob).Execute();
A separate service retrieves the report like this:
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
{
ClientId = CLIENT_ID,
ClientSecret = CLIENT_SECRET
},
new[] { YouTubeReportingService.Scope.YtAnalyticsReadonly },
"user",
CancellationToken.None,
new FileDataStore("Drive.Auth.Store")).Result;
YouTubeReportingService reportingService = new YouTubeReportingService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = APPLICATION_NAME
});
// Retrieve data from jobs that were previously submitted.
ListJobsResponse jobList = reportingService.Jobs.List().Execute();
if (jobList.Jobs != null)
{
foreach (Job job in jobList.Jobs)
{
ListReportsResponse reportList = reportingService.Jobs.Reports.List(job.Id).Execute();
if (reportList.Reports != null)
{
foreach (Report report in reportList.Reports)
{
MediaResource.DownloadRequest getRequest = reportingService.Media.Download("");
// Download the report data.
using (MemoryStream stream = new MemoryStream())
{
getRequest.MediaDownloader.Download(report.DownloadUrl, stream);
stream.Flush();
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
// Parse report...
DataTable parsedReport = ReportToDataTable(reader.ReadToEnd());
// ...and get data on videos watched and videos clicked to.
foreach (DataRow row in parsedReport.Rows)
{
string videoWatched = row["video_id"].ToString();
string videoClickedToFromEndScreen = **WHAT???**
}
}
}
}
}
}
}
When uploading videos to YouTube using the YouTube api v3, I get the following error:
Google.Apis.Requests.RequestError
Access forbidden. The request may not be properly authorized. [403]
Errors [
Message[Access forbidden. The request may not be properly authorized.] Location[ - ] Reason[forbidden] Domain[youtube.common]
]
The app will successfully upload a couple videos, then randomly throws this 403 error.
The quota limit is 1 millon queries per day, I have only used about 1%.
There are approximately 1200 videos already on the channel that were uploaded using the same app, now all of a sudden in the last few days I am getting this error. I've tried uploading videos to another channel and had no problems, so it looks as though the issue is specific to this one channel. Seems as though it's being throttled, but I don't know why?
I have not contravened any terms of service, the videos are original, the account is in good standing and I'm within the quota limit.
If there are any other limits, why does Google not document it in the api refrence? That way we can design our apps according to policy.
It makes it impossible to build an app if you follow spec and then Google have a different set of rules on the backend and you are left guessing what you are and aren't allowed to do.
Using this code:
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;
namespace Google.Apis.YouTube.Samples
{
/// <summary>
/// YouTube Data API v3 sample: upload a video.
/// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
/// See https://developers.google.com/api-client-library/dotnet/get_started
/// </summary>
internal class UploadVideo
{
[STAThread]
static void Main(string[] args)
{
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();
}
private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.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 = "Default Video Title";
video.Snippet.Description = "Default Video Description";
video.Snippet.Tags = new string[] { "tag1", "tag2" };
video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
var filePath = #"REPLACE_ME.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);
}
}
}
I have to retrieve all video of my channel with Youtube API.
All videos are published on Youtube and I can see them correctly.
I tried to make the request directly from this page:
https://developers.google.com/youtube/v3/docs/search/list
and this is the example request:
GET http s://www.googleapis.com/youtube/v3/search?part=snippet&channelId=myChannelID&maxResults=50&key={YOUR_API_KEY}
Request doesn't retrieve all videos, it returns only 7 on the total of 9.
All videos have the same configuration. Missing videos are always the same.
If I use the video API passing the ID of one of those videos excluded from the search response, it returns a correct response and it belong correctly to my channel:
https://developers.google.com/youtube/v3/docs/videos/list#try-it
Someone can help me?
thank you in advance
Francesco
The answer to "How do I obtain a list of all videos in a channel using the YouTube Data API v3?" here may be what you need. Look especially at the video linked to in the answer.
To summarize, to get all the uploads from a channel, you need to get the items from the uploads playlist for the channel using playlistItems.list on that playlist's ID rather than calling search.list on the channel ID.
Try this two-step approach:
Get the ID of your channel's uploads playlist using the channels.list API call: GET https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id={YOUR_CHANNEL_ID}&key={YOUR_API_KEY}
Get the videos from the uploads playlist using the playlistItems.list call: GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=3&playlistId={YOUR_PLAYLIST_ID}&key={YOUR_API_KEY}
try this
async static Task<IEnumerable<YouTubeVideo>> GetVideosList(Configurations configurations, string searchText = "", int maxResult = 20)
{
List<YouTubeVideo> videos = new List<YouTubeVideo>();
using (var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = configurations.ApiKey
}))
{
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchText;
searchListRequest.MaxResults = maxResult;
searchListRequest.ChannelId = configurations.ChannelId;
searchListRequest.Type = "video";
searchListRequest.Order = SearchResource.ListRequest.OrderEnum.Date;// Relevance;
var searchListResponse = await searchListRequest.ExecuteAsync();
foreach (var responseVideo in searchListResponse.Items)
{
videos.Add(new YouTubeVideo()
{
Id = responseVideo.Id.VideoId,
Description = responseVideo.Snippet.Description,
Title = responseVideo.Snippet.Title,
Picture = GetMainImg(responseVideo.Snippet.Thumbnails),
Thumbnail = GetThumbnailImg(responseVideo.Snippet.Thumbnails)
});
}
return videos;
}
}