Salesforceの添付データを取得し、新規フォルダを作成ご保存数するプログラムを作っているのですが、下記のエラーを解決できず詰まっています。
File "autotmp.py", line 50, in <module>
resp = sfdc.get(urlpath).decode('utf-8')
File "/usr/local/lib/python3.5/dist-packages/salesforce/api.py", line 49, in get
return self.__get_api(soap).get(get_url, params)
File "/home/dbcon/utils.py", line 188, in authenticate_and_call
return func(self, *args, **kwargs)
File "/home/dbcon/salesforceRestApi.py", line 104, in get
params=params)
File "/home/dbcon/salesforceRestApi.py", line 137, in __send_request
**kwargs)
File "/home/dbcon/utils.py", line 159, in send_request
resp = response.json()
File "/usr/lib/python3/dist-packages/requests/models.py", line 740, in json
return json.loads(self.content.decode(encoding), **kwargs)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
どなたか解決策を教えていただけないでしょうか。
宜しくお願いします。
python3.5
1 2#!/usr/bin/env python 3# -*- coding: utf-8 -*- 4#Automatically download attached files from records 5#To download multiple files as zip use: 6# https://fsi-america-dev-ed.my.salesforce.com/sfc/servlet.shepherd/document/download/<id>/<id>/... 7 8import salesforce as sf 9import os 10import urllib.request 11 12sfdc = sf.Salesforce() 13sfdc.authenticate(client_id=client_id,client_secret=client_secret,username=username,password=password) 14 15#get api sobjects 16api_objects = [] 17urlpath = "/sobjects/" 18resp = sfdc.get(urlpath,'utf-8') 19for record in resp["sobjects"]: 20 api_objects.append(record["name"]) 21 22#get id, filename from contentversion 23version = {} 24query_results = sfdc.query("SELECT Id, ContentDocumentId, Title, FileExtension FROM ContentVersion") 25for record in query_results["records"]: 26 if record["ContentDocumentId"] not in version: 27 version[record["ContentDocumentId"]] = [] 28 version[record["ContentDocumentId"]].append({"Id":record["Id"],"Filename":record["Title"] + "." + record["FileExtension"]}) 29 30#download 31for api_object in api_objects: 32 print("processing:" + api_object, 'utf-8') 33 query_results = None 34 try: 35 query_results = sfdc.query("SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink where LinkedEntityId in (SELECT Id FROM " + api_object + ")") 36 except: 37 continue 38 for record in query_results["records"]: 39 if record["ContentDocumentId"] in version: 40 vlist = version[record["ContentDocumentId"]] 41 for v in vlist: 42 print(" downloading:" + v["Id"], 'utf-8') 43 urlpath = "/sobjects/ContentVersion/" + v["Id"] + "/VersionData" 44 filepath = base_folder + api_object + "/" + record["LinkedEntityId"] + "/" + v["Filename"] 45 resp = sfdc.get(urlpath, 'utf-8') 46 os.makedirs(os.path.dirname(filepath), exist_ok=True) 47 with open(filepath, "wb", encoding='utf-8') as f: 48 f.write(resp.content) 49 50 51 52