pythonpython-3.xpython-requestsgroupme

Routing an image through GroupMe's image service with Python


I'm using requests to make a POST request to GroupMe's image service that should return a URL of the hosted image that I can use to post to a GroupMe thread. The documentation mentions that I need my access token and the binary image data in the payload in order to do this.

Here is a very simple example of how my code to do this currently looks:

import requests

access_token = 'my_access_token'
img_path = 'picture_name.jpg'

img_service_url = 'https://image.groupme.com/pictures'

r = requests.post(img_service_url, files={'file': img_path})

EDIT:

I looked at the documentation and source for the groupy.api.endpoint module Groupy(https://groupy.readthedocs.io/en/v0.6.2/_modules/groupy/api/endpoint.html#Images) and updated my script (reflected above) to use the same requests function parameters, but to no avail. Now the code returns a 500.


Solution

  • This worked for me (avatar.jpeg is in the same folder as my testing.py code below)

    # curl 'https://image.groupme.com/pictures'
    # -X POST
    # -H "X-Access-Token: $GM_TOKEN"
    # -H "Content-Type: image/jpeg"
    # --data-binary @AwesomePicture.jpg
    
    import requests
    
    data = open('./avatar.jpeg', 'rb').read()
    res = requests.post(url='https://image.groupme.com/pictures',
                        data=data,
                        headers={'Content-Type': 'image/jpeg',
                                 'X-Access-Token': 'ACCESS_TOKEN'})
    print(res.content)
    

    OUTPUT

    b'{"payload":{"url":"https://i.groupme.com/100x100.jpeg.5b71f15633f6454ca6a3a6b3e267a3fb","picture_url":"https://i.groupme.com/100x100.jpeg.5b71f15633f6454ca6a3a6b3e267a3fb"}}\n'