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