WData Query API returns 500 Error
回答済みI'm attempting to use the API with WData and getting `500` errors that aren't helpful. FWIW, API works fine with Tasks and Users api calls.
Am I using the correct syntax?
query_url = 'https://h.app.wdesk.com/s/wdata/prep/api/v1/query'
headers = get_headers() # Python def to build the headers
r = requests.get(query_url, headers = headers)
print(r.status_code)
# returns 500
-
Hi Nick, the error is likely somewhere not included in the code snippet. Generate a valid bearer token and try the following code. Pass the value of the valid bearer token into "bearerToken".
url = "https://h.app.wdesk.com/s/wdata/prep/api/v1/query"
payload = {}
headers = {
'Accept': 'application/json',
'Authorization': bearerToken
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)0Hey Jeff - yep, I've got a good bearer token that works with other Workiva API's, and I refresh it for each run.
0Here's the helper function that creates the header:
def get_headers(accept_flag='application/vnd.api+json'):
bearer_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'charset': 'UTF-8'
}
bearer_url = 'https://' + host + path
bearer_data = {'client_id':client_id, 'client_secret':secret, 'grant_type':'client_credentials'}
bearer_result = requests.post(bearer_url, data= bearer_data, headers=bearer_headers)
bearer_json = json.loads(bearer_result.text)
bearer_token = bearer_json['access_token']
user_headers = {
'Accept': accept_flag,
'Authorization': 'Bearer ' + bearer_token
}
return user_headers0Hi Nick. To make sure we are on the same page, are you attempting to run the Retrieve List of Queries endpoint? If so, please test the example code in my prior post. I can confirm that python code will successfully pull a list of queries. You can use the following function as an example to get a valid bearer token:
def get_access_token(client_id, client_secret):
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
response = requests.post('https://api.app.wdesk.com/iam/v1/oauth2/token', headers=headers, data={'client_id': client_id,'client_secret': client_secret,'grant_type':'client_credentials'})
response_body = response.json()
return 'Bearer ' + response_body['access_token']
bearerToken = get_access_token("<Client_ID>", "<Client_Secret>")If you use Postman, to get a jump start on learning and using both the IAM and Wdata APIs, I recommend testing with the Postman Collections for these two APIs.
1Hi Nick,
I've tested your get_headers() method. It also returns a 500 to me. I made it work by removing the "accept_flag" field from the header.
def get_headers():
bearer_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'charset': 'UTF-8'
}
bearer_url = 'https://' + host + path
bearer_data = {'client_id':client_id, 'client_secret':secret, 'grant_type':'client_credentials'}
bearer_result = requests.post(bearer_url, data= bearer_data, headers=bearer_headers)
bearer_json = json.loads(bearer_result.text)
bearer_token = bearer_json['access_token']
user_headers = {
'Authorization': 'Bearer ' + bearer_token
}
return user_headersLet me know if that works for you.
1Jeff Hickey Thanks for the shout out to Postman - we have a careful relationship with that tool in our company but I'll explore it.
Jesus Bouzada Removing the `accept_flag` worked. I tweaked the get_headers def to make it optional, so removing it returned a 200 response and all the data. So much data. :)
0
コメント
7件のコメント