Multi-page POST Google Glass request

I am trying to add an attachment to my timeline using multi-line encoding. I did something like the following:

req = urllib2.Request(url,data={body}, header={header}) resp = urllib2.urlopen(req).read() 

And it works great for application / json. However, I'm not sure how to format the body for multipart. I also used some libraries: requests and a poster, and they both return 401 for some reason.

How can I make a multi-page request either with libary (preferably a plugin for urllib2) or with urllib2 itself (for example, the code block above)?

EDIT: I would also like this to support the mirror-api "video / vnd.google-glass.stream-url" from https://developers.google.com/glass/timeline

For the query using the poster library, here is the code:

 register_openers() datagen, headers = multipart_encode({'image1':open('555.jpg', 'rb')}) 

Details are used here:

 headers = {'Authorization' : 'Bearer %s' % access_token} files = {'file': open('555.jpg', 'rb')} r = requests.post(timeline_url,files=files, headers=headers) 

Returns 401 -> Header

thanks

+4
source share
3 answers

Here's how I did it and how the python client library does it.

 from email.mime.multipart import MIMEMultipart from email.mime.nonmultipart import MIMENonMultipart from email.mime.image import MIMEImage mime_root = MIMEMultipart('related', '===============xxxxxxxxxxxxx==') headers= {'Content-Type': 'multipart/related; ' 'boundary="%s"' % mime_root.get_boundary(), 'Authorization':'Bearer %s' % access_token} setattr(mime_root, '_write_headers', lambda self: None) #Create the metadata part of the MIME mime_text = MIMENonMultipart(*['application','json']) mime_text.set_payload("{'text':'waddup doe!'}") print "Attaching the json" mime_root.attach(mime_text) if method == 'Image': #DO Image file_upload = open('555.jpg', 'rb') mime_image = MIMENonMultipart(*['image', 'jpeg']) #add the required header mime_image['Content-Transfer-Encoding'] = 'binary' #read the file as binary mime_image.set_payload(file_upload.read()) print "attaching the jpeg" mime_root.attach(mime_image) elif method == 'Video': mime_video = MIMENonMultipart(*['video', 'vnd.google-glass.stream-url']) #add the payload mime_video.set_payload('https://dl.dropboxusercontent.com/u/6562706/sweetie-wobbly-cat-720p.mp4') mime_root.attach(mime_video) 

Mark Scheel I used your video for testing :) Thanks.

0
source

There is an example of Curl working for a multi-page request that uses the streaming video URL function:

Previous answer streaming video with an example of twisting

It does exactly what you are trying to do, but using Curl. You just need to adapt this to your technology stack.

The 401 you get will discourage you, even if you use the correct syntax. Answer 401 indicates that you do not have permission to change the timeline. Make sure you can only insert the first welcome text card first. After you overcome error 401 and move on to parsing and formatting errors, the link above should be all you need.

In the last post, you donโ€™t need urllib2 , the Mirror API team dropped the function gem on our lap and we donโ€™t need to worry about getting the binary video file, check this example linked above. I just provided the url in a multi-page payload, no need to pass binary data! Google does all the magic in XE6 and above for us.

Thanks Team Glass!

I think you will find it easier than you think. Try a twist example and watch for incompatible types of videos when you go this far, if you don't use a compatible type, it doesn't seem to work in Glass, make sure your video is encoded in a format that is easy to read on the stack.

Good luck

+1
source

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

+1
source

Source: https://habr.com/ru/post/1490373/


All Articles