It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to post a .png file to a hipchat room using python?

I have an image file my_image.png saved in a local directory, and I am trying to post that file to a hipchat room.

I found this and tried it but got the below error:

{
"error": {
"code": 403,
"message": "Only requests matching one of these authenticated principals are allowed to access this resource: an active user",
"type": "Forbidden"
}

I also created a python function after looking around the internet below:

def hipchat_send_file(image_file, token=TOKEN, room=ROOM, color='green', notify=False, host='myhostname.com'):
url = "https://{0}/v2/room/{1}/share/file".format(host, room)
headers = {'Content-Type': 'multipart/related; boundary=boundary123456'}
headers['Authorization'] = "Bearer " + token


files = {'file': (open(image_file, 'rb'), 'image/png')}

r = requests.post(url, files=files, headers=headers)
print(r.status_code)
print(r.text)

and I got the same exact error as above:

{
"error": {
"code": 403,
"message": "Only requests matching one of these authenticated principals are allowed to access this resource: an active user",
"type": "Forbidden"
}

Can anyone help?  Thanks.

2 answers

0 votes
MB Atlassian Team May 22, 2018

Hi Patricia,

The error you're getting suggests a issue with the API Token. It does suggest the user account the token was generated from is not active or enabled. 

You can login to the Web UI and under your profile generate a new v2 API Token with the permissions needed ( multi-select ) and replace your token. 

Cheers,
Max

Thanks Max for the suggestion. 

So, I got a new token and I selected all the scopes.

It gave a different error and suggested to visit this page, which I did.

I found this link to help with how to use the suggested link above.

Now I have below python function:

def hipchat_send_file(filepath, token=TOKEN, room=ROOM, host='hipchat.my_host_name.com'):

""" Send file to a HipChat room via API version 2
Parameters
----------
token : str
HipChat API version 2 compatible token - must be token for active user
room: str
Name or API ID of the room to notify
filepath: str
Full path of file to be sent
message: str, optional
Message to send to room
host: str, optional
Host to connect to, defaults to api.hipchat.com
"""

if not os.path.isfile(filepath):
raise ValueError("File '{0}' does not exist".format(filepath))

url = "https://{0}/v2/room/{1}/share/file".format(host, room)
headers = {'Content-type': 'multipart/related; boundary=boundary123456'}
headers['Authorization'] = "Bearer " + token

payload = """\
--boundary123456
Content-Type: application/json; charset=UTF-8
Content-Disposition: attachment; name="metadata"
--boundary123456
Content-Type: image/png
Content-Disposition: attachment; name="file"; filename="{0}"
{1}
--boundary123456--\
""".format(os.path.basename(filepath), open(filepath, 'rb'))

r = requests.post(url, headers=headers, data=payload)
r.raise_for_status()

and got the following error when I ran it:

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://hipchat.my_host_name.com/v2/room/my_room_id/share/file

 I'm not sure what else to do...

Found this, tried it out and worked!!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Hipchat

Hipchat Cloud and Stride have reached End of Life (updated)

All good things come to an end - thanks to all our customers and partners who have been along the Hipchat and Stride journey with us.  As of Feb 15th 2019, Hipchat Cloud and Stride have reached ...

35,536 views 9 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you