I'm trying to upload a .pdf file in the content of my Portal for ArcGIS with a python script. I'm trying with the "additem" REST operation. My code is as follows:
import urllib
import urllib2
import json
import argparse
import os
import json
sourcePortal='https://xxx/arcgis'
def generateToken(username, password, portalUrl):
'''Retrieves a token to be used with API requests.'''
parameters = urllib.urlencode({'username' : username,
'password' : password,
'client' : 'referer',
'referer': portalUrl,
'expiration': 60,
'f' : 'json'})
response = urllib2.urlopen(portalUrl + '/sharing/rest/generateToken?',
parameters).read()
try:
jsonResponse = json.loads(response)
if 'token' in jsonResponse:
print("Token generated OK")
return jsonResponse['token']
elif 'error' in jsonResponse:
print jsonResponse['error']['message']
for detail in jsonResponse['error']['details']:
print detail
except ValueError, e:
print 'An unspecified error occurred.'
print e
def uploadFile(token):
'''Creates a new item in a user's content.'''
parameters = urllib.urlencode({'title' : 'myreport',
'overwrite': 'true',
'type' : 'PDF',
'token' : token,
'async' : 'true',
'multipart' : 'true',
'filename' : 'newfile',
'f' : 'json'
})
response = urllib2.urlopen('https://xxx/arcgis/sharing/content/users/admin/additem',
parameters).read()
return response
sourceToken = generateToken(username='xxx', password='xxx',
portalUrl=sourcePortal)
reply=uploadFile(sourceToken)
itemID = json.loads(reply)["id"]
pdfFile = open('C:\\xxx\\xxx.pdf')
parameters = urllib.urlencode({
'file' : pdfFile,
'partNum': '1',
'token' : sourceToken,
'f' : 'json'})
print(urllib2.urlopen('https://xxx/arcgis/sharing/content/users/admin/items/'+ itemID + "/addpart",parameters).read())
parameters = urllib.urlencode({
'token' : sourceToken,
'f' : 'json'})
print(urllib2.urlopen('https://xxx/arcgis/sharing/content/users/admin/items/'+ itemID + "/commit",parameters).read())
I have a problem with the "addpart". Here is the message :
{"code":400,"message":"Unable to add part.","details":["Invalid or missing 'file' parameter."]}
Does anyone have an idea to figure this out?
Aucun commentaire:
Enregistrer un commentaire