我正在尝试使用 .NET Mirror API 附加视频流,但遇到了一些麻烦。
我似乎找不到支持此处引用的格式的方法:
POST /upload/mirror/v1/timeline HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer {auth token}
Content-Type: multipart/related; boundary="mymultipartboundary"
Content-Length: {length}
--mymultipartboundary
Content-Type: application/json; charset=UTF-8
{ "text": "Skateboarding kittens" }
--mymultipartboundary
Content-Type: video/vnd.google-glass.stream-url
http://example.com/path/to/kittens.mp4
--mymultipartboundary--
我见过的谷歌最好的信息是使用以下方法进行插入:
/// <summary>
/// Insert a new timeline item in the user's glass with an optional
/// notification and attachment.
/// </summary>
/// <param name='service'>Authorized Mirror service.</param>
/// <param name='text'>Timeline Item's text.</param>
/// <param name='contentType'>
/// Optional attachment's content type (supported content types are
/// "image/*", "video/*" and "audio/*").
/// </param>
/// <param name='attachment'>Optional attachment stream</param>
/// <param name='notificationLevel'>
/// Optional notification level, supported values are null and
/// "AUDIO_ONLY".
/// </param>
/// <returns>
/// Inserted timeline item on success, null otherwise.
/// </returns>
public static TimelineItem InsertTimelineItem(MirrorService service,
String text, String contentType, Stream attachment,
String notificationLevel) {
TimelineItem timelineItem = new TimelineItem();
timelineItem.Text = text;
if (!String.IsNullOrEmpty(notificationLevel)) {
timelineItem.Notification = new NotificationConfig() {
Level = notificationLevel
};
}
try {
if (!String.IsNullOrEmpty(contentType) && attachment != null) {
// Insert both metadata and media.
TimelineResource.InsertMediaUpload request = service.Timeline.Insert(
timelineItem, attachment, contentType);
request.Upload();
return request.ResponseBody;
} else {
// Insert metadata only.
return service.Timeline.Insert(timelineItem).Fetch();
}
} catch (Exception e) {
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
}
但是,此代码将内容“附加”为流(这对于上传图像非常有用,我已经对其进行了测试并进行了工作)。但是,流式视频只需要视频的 URL。
我尝试将 URL 的字符串表示形式作为流发送,但结果只是一个无限加载的视频。
我已经能够通过使用我的身份验证令牌和上面的 POST 请求发出 cURL 请求来成功播放视频,所以我知道视频本身不是问题。
有没有人能够让流视频通过 .NET 工作(使用 Mirror API 或使用某种自定义 WebRequest?)我尝试自己从头开始创建 WebRequest,但我得到了 400 个作为响应。
作为参考,我尝试过的其他代码:
var request = WebRequest.CreateHttp(baseAddress + method);
request.Method = "POST";
request.Headers.Add("Authorization", string.Format("Bearer {0}", auth));
string itemJson = JsonConvert.SerializeObject(item.Item, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
string contentFormat = "--MyBound\nContent-Type: application/json; charset=UTF-8\n\n{0}\n--MyBound\nContent-Type: video/vnd.google-glass.stream-url\n\n{1}\n--MyBound--";
string content = string.Format(contentFormat, new[] { itemJson, item.VideoUrl });
request.ContentLength = content.Length;
request.ContentType = "multipart/related; boundary=\"MyBound\"";
var rs = request.GetRequestStream();
using (var sw = new StreamWriter(rs))
{
sw.Write(content);
}
var response = request.GetResponse();
item 是我编写的一个类,其中包含作为字符串的 VideoUrl 和 Item(来自 Mirror API 的 TimelineItem),并且我使用的视频 Url 是:
http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8
提前谢谢大家!