Upload Files by API with Python

Hello,
I`m Thomas from Germany, I use this great Software for round about 6 Years,
now I have the chalange to automatic a file upload to a layout every day.

I´m new in Python,
my skript works fine with the Authorization and I can read out the old Media ID I will change,
but when I try to Upload the new file, by Py Skript the following massage is the result.

Python 3.7.0b2 (v3.7.0b2:b0ef5c979b, Feb 28 2018, 02:24:20) [MSC v.1912 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

================ RESTART: D:/XIBO_SKRIPT/upload_to_layout.py ================
{"files":[{"name":"hbm.png","size":0,"type":"image\/png","error":"File is too small"}]}

in Postman it works fine.

hope you can help me.

Thanks

Thomas

Could you tell me what CMS version are you using?

ie is it the 1.7 and earlier API or current 1.8 series API that you’re trying to use?

Perhaps you could show us a snippet of the script that you’re using?

Hi Peter,

sorry for my late answer,

I use Version 1.8.7.
her is my skript sorry I´m newbie
the access works and it shows me the old ID

import requests, json
from pprint import pprint

token_url = “http://myxiboadress/api/authorize/access_token

test_api_url = “http://ds.media-brueggemann.de/api/

#client credentials
client_id = 'xxxxx’
client_secret = ‘xxxxx’

#step A, - single call with client credentials as the basic auth header - will return access_token
data = {‘grant_type’: ‘client_credentials’}

access_token_response = requests.post(token_url, data=data, verify=False, allow_redirects=False, auth=(client_id, client_secret))

tokens = json.loads(access_token_response.text)

##print(access_token_response.text)
print("Bearer " + tokens[‘access_token’])

#Abfrage Media ID

url1 = “http://myxiboadress/api/library

querystring = {“media”:“CTV_Sendung”}

headers = {
‘Authorization’: "Bearer " + tokens[‘access_token’],
‘Cache-Control’: “no-cache”,
}

mediaId_response = requests.get(url1, headers=headers, params=querystring)

mediaId = json.loads(mediaId_response.text)

print(mediaId[0][‘mediaId’])

#Upload File

payload = “------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=“files”; filename=“C:\test.JPG”\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=“name”\r\n\r\nCTV_Sendung\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=“oldMediaId”\r\n\r\n156\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=“updateInLayouts”\r\n\r\n1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=“deleteOldRevisions”\r\n\r\n1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW–”

headers = {
‘content-type’: “multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW”,
‘Cache-Control’: “no-cache”,
‘Content-Type’: “application/json”,
‘Postman-Token’: “a7c524e4-dac6-442c-8387-f9c7605cd1a8”,
}

response = requests.request(“POST”, url1, data=payload, headers=headers)

print(response.text)

Hi did anybody have a solution for me ?

Perhaps you could have a look here https://github.com/xibosignage/oauth2-xibo-cms/blob/master/example/file.php

You could make your life even easier using the wrapper, as then you’d only need to call this to upload a file - example usage.