0
def get_token():
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    data = {'client_id': 'XXX', 'client_secret': 'XXX', 'grant_type': 'client_credentials', 'scope': 'data:read'}
    response = requests.post('https://developer.api.autodesk.com/authentication/v1/authenticate', headers=headers, data=data)
    return response.json()['access_token']

print('Bearer ' + get_token())
response_form_templates = requests.get('https://developer.api.autodesk.com/construction/forms/v1/projects/:projectId/form-templates', headers={'Authorization': 'Bearer ' + get_token()})
print(response_form_templates.json())
print(response_form_templates)

I'm trying to use python requests to simply get back the form-templates and all I'm getting back is a 401 response and a message saying "Authorization failed". The few print statements in there are from my trying to debug what's going wrong, but with my client_id and client_secret in there, it gives me those errors.

Any idea as to what could be wrong would be really helpful, thanks.

6
  • I see you print a bearer token. Does the server where you post use bearer authorization token? If yes, you need to include the authorization token in the headers headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': f'Bearer {get_token()}'} Commented Mar 15, 2022 at 19:06
  • @AlexandruPlacinta It does, the server sends back a bearer token. The authorization token is included in the headers of the get request. That just updated, let me give that a shot. Commented Mar 15, 2022 at 19:09
  • My bad. I did not see. Do you get a cookie too? Commented Mar 15, 2022 at 19:10
  • @AlexandruPlacinta No cookie is sent, the Forge API docs just say you need to set the scope to data:read for the endpoint I'm hitting and then pass in the access_token. Commented Mar 15, 2022 at 19:12
  • 1
    You have projects/:projectId. Don't you need to set the :projectId? Commented Mar 15, 2022 at 19:16

2 Answers 2

1

Btw. we don't have an official Forge SDK for Python yet but there's a simple, unofficial one here: https://github.com/petrbroz/forge-sdk-python that you could perhaps use as a reference when building the HTTP requests yourself. For example, here's how the SDK retrieves the 2-legged access token: https://github.com/petrbroz/forge-sdk-python/blob/develop/src/autodesk_forge_sdk/auth.py#L147-L178.

Sign up to request clarification or add additional context in comments.

Comments

1

You are using a 2-legged token instead of a 3-legged token. As you can see from the image below, you require a 3-legged token when you want to retrieve form templates.

enter image description here

Use this link and see how you can get a 3-legged token.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.