Expand my Community achievements bar.

Latest Community Ideas Review is Out: Discover What’s New and What to Expect!
SOLVED

Python code to access Workfront API using OAuth2 authenticaiton

Avatar

Level 10

Hello,

 

Is there anybody, who could provide me with some Python code to access the Workfront API using OAuth2 authorization code flow?

 

I am currently too dump to make some progress on this issue...

 

Thanks in advance.

 

Regards

Lars

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi @lgaertner 


from requests_oauthlib import OAuth2Session


authorization_base_url = "https://<your-domain>.my.workfront.com/attask/oauth/authorize"
token_url = "https://<your-domain>.my.workfront.com/attask/oauth/token"


client_id = "<your-client-id>" # Replace with your client ID
redirect_uri = "<your-redirect-URI>" # Replace with your redirect URI


oauth = OAuth2Session(client_id, redirect_uri=redirect_uri)


authorization_url, state = oauth.authorization_url(authorization_base_url)
webbrowser.open(authorization_url)


authorization_response = input('Enter the full callback URL')


token = oauth.fetch_token(
token_url,
authorization_response=authorization_response,
client_secret="<your-client-secret>") # Replace with your client secret

 

This is to get the User Profile:
response = oauth.get('https://<your-domain>.my.workfront.com/attask/api/user/profile')
print(response.json())

View solution in original post

3 Replies

Avatar

Correct answer by
Level 4

Hi @lgaertner 


from requests_oauthlib import OAuth2Session


authorization_base_url = "https://<your-domain>.my.workfront.com/attask/oauth/authorize"
token_url = "https://<your-domain>.my.workfront.com/attask/oauth/token"


client_id = "<your-client-id>" # Replace with your client ID
redirect_uri = "<your-redirect-URI>" # Replace with your redirect URI


oauth = OAuth2Session(client_id, redirect_uri=redirect_uri)


authorization_url, state = oauth.authorization_url(authorization_base_url)
webbrowser.open(authorization_url)


authorization_response = input('Enter the full callback URL')


token = oauth.fetch_token(
token_url,
authorization_response=authorization_response,
client_secret="<your-client-secret>") # Replace with your client secret

 

This is to get the User Profile:
response = oauth.get('https://<your-domain>.my.workfront.com/attask/api/user/profile')
print(response.json())

Avatar

Level 10

Hello Jvenkat,

 

Thanks a lot. Unfortunately it is not working for me yet...

 

I constantly do get the error message Missing access token parameter.

 

What do you use as redirect_uri and how is your callback URL formatted?

 

Regards

Lars

Avatar

Level 10

Correction:

 

I got it finally working!!!

 

Here is my final code:

 

import webbrowser
from requests_oauthlib import OAuth2Session

# Client-side Configuration
client_id = "<clientID>" # Replace with your client ID
client_secret = "<clientSecret>"
redirect_uri = "http://127.0.0.1:8088" # Replace with your redirect URI

# OAuth endpoints given in the Workfront API documentation
authorization_base_url = 'https://oauth.my.workfront.com/integrations/oauth2/authorize'
token_url = 'https://<your-domain>.my.workfront.com/integrations/oauth2/api/v1/token'
response_type = 'code'


# Create the OAuth2 Session
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri)

# Redirect user to Workfront for authorization
authorization_url, state = oauth.authorization_url(authorization_base_url)
webbrowser.open(authorization_url)

# User will get an authorization code. Copy it
authorization_response = input('Enter the full callback URL: ')


# Exchange the authorization code for a token
token = oauth.fetch_token(
token_url,
authorization_response=authorization_response,
client_secret = client_secret)

print(token)

 

Thanks a lot @Jvenkat