如何使用.NET在Mirror API中下载共享映像

时间:2013-10-09 03:17:40

标签: google-mirror-api google-glass

我已在我的Glassware应用程序中订阅了时间线通知。当用户与Glassware竞赛共享图像时,我收到了通知。现在我需要将该图像下载到我的Glassware应用程序进行处理。

 Notification notification =
        new NewtonsoftJsonSerializer().Deserialize<Notification>(Request.InputStream);
    String userId = notification.UserToken;
    MirrorService service = new MirrorService(new BaseClientService.Initializer()
    {
        Authenticator = Utils.GetAuthenticatorFromState(Utils.GetStoredCredentials(userId))
    });

    if (notification.Collection == "timeline")
        {
        foreach (UserAction action in notification.UserActions)
        {
        if (action.Type == "SHARE")
            {
            TimelineItem item = service.Timeline.Get(notification.ItemId).Fetch();

             //i have to download content here.

            break;
            }
        else
            {
            Console.WriteLine(
                "I don't know what to do with this notification: " + action.ToString());
            }
        }
        }

1 个答案:

答案 0 :(得分:4)

reference guide

中描述了下载附件
using System;
using Google.Apis.Mirror.v1;
using Google.Apis.Mirror.v1.Data;
using System.Net;
using System.IO;

public class MyClass {
  // ...

  /// <summary>
  /// Print an attachment's metadata.
  /// </summary>
  /// <param name="service">Authorized Mirror service.</param>
  /// <param name="itemId">ID of the timeline item the attachment belongs to.</param>
  /// <param name="attachmentId">ID of the attachment to print metadata for.</param>
  public static void PrintAttachmentMetadata(
      MirrorService service, String itemId, String attachmentId) {
    try {
      Attachment attachment = service.Timeline.Attachments.Get(itemId, attachmentId).Fetch();

      Console.WriteLine("Attachment content type: " + attachment.ContentType);
      Console.WriteLine("Attachment content URL: " + attachment.ContentUrl);
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
    }
  }

  /// <summary>
  /// Download a timeline items's attachment.
  /// </summary>
  /// <param name="service">Authorized Mirror service.</param>
  /// <param name="attachment">Attachment to download content for.</param>
  /// <returns>Attachment's content if successful, null otherwise.</returns>
  public static System.IO.Stream DownloadAttachment(
      MirrorService service, Attachment attachment) {
    try {
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
        new Uri(attachment.ContentUrl));
      service.Authenticator.ApplyAuthenticationToRequest(request);
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      if (response.StatusCode == HttpStatusCode.OK) {
        return response.GetResponseStream();
      } else {
        Console.WriteLine(
          "An error occurred: " + response.StatusDescription);
        return null;
      }
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
      return null;
    }
  }

  // ...
}