How to add an attachment to a multi-line encoded timeline:
The easiest way to add multi-page encoding attachments to the timeline is to use the Google APIs API Client Library for Python . Using this library, you can simply use the following sample code provided in the documentation for setting the Mirror API timeline (click the Python tab in the Examples section).
from apiclient.discovery import build service = build('mirror', 'v1') def insert_timeline_item(service, text, content_type=None, attachment=None, notification_level=None): timeline_item = {'text': text} media_body = None if notification_level: timeline_item['notification'] = {'level': notification_level} if content_type and attachment: media_body = MediaIoBaseUpload( io.BytesIO(attachment), mimetype=content_type, resumable=True) try: return service.timeline().insert( body=timeline_item, media_body=media_body).execute() except errors.HttpError, error: print 'An error occurred: %s' % error
In fact, you cannot use requests or poster to automatically encode your data, as these libraries encode objects in multipart/form-data , whereas the Mirror API wants things in multipart/related .
How to debug the current error code:
Your code gives 401, which is an authorization error. This means that you probably do not include your access token with your requests. To enable an access token, set the Authorization field to Bearer: YOUR_ACCESS_TOKEN in your request (documentation here ).
If you do not know how to get an access token, there is a page here in the Glass developer docs that explains how to get an access token. Make sure that your authorization process has requested the following opportunity for multipart-upload, otherwise you will receive error 403. https://www.googleapis.com/auth/glass.timeline
source share