- Mark as New
- Bookmark
- Subscribe
- Permalink
- Email to a Friend
- Report Inappropriate Content
Data Query Post using API in python
Hello All , Can any one suggest how I can post a data query using python and rest API and download data as CSV. I'm getting 400 bad request error when I try to run below sample
import httplib2,json,http.client,urllib.request,csv,time,os,datetime,sys,logging,subprocess
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
httplib2.debuglevel = 1
h = httplib2.Http('.cache', disable_ssl_certificate_validation=True)
login_headers = {"Content-Type": "application/json",
"apiAccessKeyId": 'username',
"apiSecretAccessKey": 'pwd',}
services_login_url = "https://rest.zuora.com/"
resp, content = h.request(services_login_url, "POST", headers=login_headers)
cookie = resp["set-cookie"]
export_excel_headers = {"Content-Type": "application/json",
"Cookie": cookie}
create_export_excel_url = "https://rest.zuora.com/query/jobs"
createexp = "{\n \"Format\": \"csv\", \n \"Name\": \"test\", \n \"Query\": \"SELECT accountnumber, balance FROM Account WHERE Account.balance > 100 \"\n}"
resp, content = h.request(create_export_excel_url, "POST",createexp , headers=export_excel_headers)
data = json.loads(content)
export_id = data.get("Id");
Thanks
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Data Query Post using API in python
First, I recommend the Requests library https://requests.readthedocs.io/en/master/.
With Data Query, you need to do the following:
- get an auth token https://www.zuora.com/developer/api-reference/#tag/OAuth
- submit a query job https://www.zuora.com/developer/api-reference/#operation/POST_DataQueryJob
- poll the query job https://www.zuora.com/developer/api-reference/#operation/GET_DataQueryJob
- the job is complete when queryStatus = 'completed', and you fetch the payload using the dataFile field