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

pawcharter May 22, 2018

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
pawcharter May 24, 2018

Found this, tried it out and worked!!

0 votes
MB
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
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

pawcharter May 23, 2018

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...

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events