Classic file types are no longer available for use as of January 2021. You can transition your classic files or download a PDF. Learn More

WData Query API returns 500 Error

Con risposta
0

Commenti

7 commenti

  • Jeff Hickey

    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)
    0
  • Nick Ryberg

    Hey Jeff - yep, I've got a good bearer token that works with other Workiva API's, and I refresh it for each run. 

    0
  • Nick Ryberg

    Here'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_headers
    0
  • Jeff Hickey

    Hi 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.

    1
  • Jesus Bouzada

    Hi 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_headers

    Let me know if that works for you.

    1
  • Nick Ryberg

    Jeff 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
  • Jesus Bouzada

    Be careful what you wish for ;)

    0

Accedi per aggiungere un commento.