# API authorization in python

**URL:** <https://forum.asana.com/t/api-authorization-in-python/82500>\
**Category:** API & Developers\
**Created:** [5 May 2020 14:04 UTC](https://forum.asana.com/t/api-authorization-in-python/82500 "2020-05-05T14:04:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon65155295](https://luna1.co/8d76cd.png) [@anon65155295](https://forum.asana.com/u/anon65155295)\
**Post date:** [5 May 2020 14:04 UTC](https://forum.asana.com/t/api-authorization-in-python/82500/1 "2020-05-05T14:04:26Z")

</div>

Hello! I’m trying to get some boards information from a asana project and I need to run (update) the script all day long. However I’m trying to authorize wiith OAuth2 (with client ID and client secret) but the authorization returns failed. When I try to authorize with bearer code the script can authorize and access the information needed. But the bearer code need to be refreshed once a hour, and I need to run this code all day long. Is there other way to authorize the script?

Python code:

#OAuth2 credentials  
client = asana.Client.oauth(  
client\_id= ‘ID’,  
client\_secret=’ SECRET’,  
redirect\_uri=‘urn:ietf:wg:oauth:2.0:oob’  
)

#Try to authorize  
(url, state) = client.session.authorization\_url()

#here returns unauthorized  
print(“authorized=”, client.session.authorized)

#Getting and trying bearer code  
try:  
# in a web app you’d redirect the user to this URL when they take action to  
# login with Asana or connect their account to Asana  
import webbrowser  
webbrowser.open(url)  
except Exception as e:  
print(“Open the following URL in a browser to authorize:”)  
print(url)  
#enter with bearer coded  
code = input(“Copy and paste the returned code from the browser and press enter:”)  
print(code)  
token = client.session.fetch\_token(code=code)

# exchange the code for a bearer token

os.environ[‘ASANA\_TOKEN’] = json.dumps(token)

#here returns authorized  
print(“authorized=”, client.session.authorized)

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [5 May 2020 15:10 UTC](https://forum.asana.com/t/api-authorization-in-python/82500/2 "2020-05-05T15:10:03Z")

</div>

Hi @anon65155295 and welcome to the forum!

There is not another way using OAuth. But if you’re just accessing your one account, you should be able to use a Personal Access Token instead - much simpler.

---

<div class="post-metadata">

**Author:** ![Peter\_Bowmar](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/peter_bowmar/32/53742_2.png) [@Peter\_Bowmar](https://forum.asana.com/u/Peter_Bowmar)\
**Post date:** [6 May 2020 15:06 UTC](https://forum.asana.com/t/api-authorization-in-python/82500/3 "2020-05-06T15:06:08Z")

</div>

Hi @anon65155295 I have exactly the same issue (see [OAuth avoid user interaction?](https://forum.asana.com/t/oauth-avoid-user-interaction/81281))

After much reading it appears the Refresh token is the way to go. I can’t use a Personal since this is an end-user tool so each user needs to access on their own merits.

Sadly I can’t find examples of dealing with the Refresh token via the Python API.

There is this: [Get refresh token - #12 by Matt\_Bramlage](https://forum.asana.com/t/get-refresh-token/19916/12)

which is exactly what I (and likely you) are hoping to do 🙂

Cheers,

Peter B

---

<div class="post-metadata">

**Author:** ![Peter\_Bowmar](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/peter_bowmar/32/53742_2.png) [@Peter\_Bowmar](https://forum.asana.com/u/Peter_Bowmar)\
**Post date:** [6 May 2020 15:08 UTC](https://forum.asana.com/t/api-authorization-in-python/82500/4 "2020-05-06T15:08:03Z")

</div>

This is useful too: [OAuth 2 Workflow — Requests-OAuthlib 1.3.1 documentation](https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#refreshing-tokens)

The pieces are there… it’s just putting them together and I don’t have the background in OAuth to intuitively come up with the code…

---

<div class="post-metadata">

**Author:** ![anon65155295](https://luna1.co/8d76cd.png) [@anon65155295](https://forum.asana.com/u/anon65155295)\
**Post date:** [6 May 2020 17:48 UTC](https://forum.asana.com/t/api-authorization-in-python/82500/5 "2020-05-06T17:48:22Z")

</div>

Thanks, Peter!  
I will see it 😃

---

<div class="post-metadata">

**Author:** ![Peter\_Bowmar](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/peter_bowmar/32/53742_2.png) [@Peter\_Bowmar](https://forum.asana.com/u/Peter_Bowmar)\
**Post date:** [6 May 2020 21:59 UTC](https://forum.asana.com/t/api-authorization-in-python/82500/6 "2020-05-06T21:59:54Z")

</div>

Unfortunately it turns out the Asana Python library doesn’t natively handle Refresh tokens yet ☹ SEe [Python API: check if authentication is still valid - #2 by Ross\_Grambo](https://forum.asana.com/t/python-api-check-if-authentication-is-still-valid/57280/2)

@anon65155295 if you happen to figure it out using raw oauthlib please let us know 🙂

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/system/32/276370_2.png) [@system](https://forum.asana.com/u/system)\
**Post date:** [30 May 2025 19:06 UTC](https://forum.asana.com/t/api-authorization-in-python/82500/7 "2025-05-30T19:06:55Z")

</div>


