3

我尝试将视频分享到我的应用程序。它收到了通知,但没有 contentUrl 来加载视频。这是通知中的附件字段:

attachments: [{contentType: 'video/mp4', 'id': 'ps:5870152408634447570'}]

isProcessingContent 字段也不存在。它尝试等待一段时间(也许视频正在处理中),但这没有任何区别。

https://developers.google.com/glass/v1/reference/timeline/attachments

有没有办法访问视频文件?

4

1 回答 1

3

contentUrl元数据中未提供附件TimelineItem,您需要向mirror.timeline.attachments.get端点发送授权请求以检索有关附件的更多信息:

from apiclient import errors
# ...

def print_attachment_metadata(service, item_id, attachment_id):
  """Print an attachment's metadata

  Args:
    service: Authorized Mirror service.
    item_id: ID of the timeline item the attachment belongs to.
    attachment_id: ID of the attachment to print metadata for.
  """
  try:
    attachment = service.timeline().attachments().get(
        itemId=item_id, attachmentId=attachment_id).execute()
    print 'Attachment content type: %s' % attachment['contentType']
    print 'Attachment content URL: %s' % attachment['contentUrl']
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

获取附件的元数据后,检查isProcessingContent属性:需要将其设置为False才能检索contentUrl. 不幸的是,当属性更改值时没有推送通知,您的服务必须使用指数退避进行轮询以节省配额和资源。

从附件的元数据中,当contentUrl可用时,您可以像这样检索附件的内容:

def download_attachment(service, attachment):
  """Download an attachment's content

  Args:
    service: Authorized Mirror service.
    attachment: Attachment's metadata.
  Returns:
    Attachment's content if successful, None otherwise.
  """
  resp, content = service._http.request(attachment['contentUrl'])
  if resp.status == 200:
    return content
  else:
    print 'An error occurred: %s' % resp
    return None
于 2013-04-24T15:41:14.187 回答