# \[Python\] \[ESI\] 401 Error When Accessing ESI

**URL:** <https://forums.eveonline.com/t/python-esi-401-error-when-accessing-esi/372905>\
**Category:** Third Party Developers\
**Created:** [August 8, 2022, 4:32pm UTC](https://forums.eveonline.com/t/python-esi-401-error-when-accessing-esi/372905 "2022-08-08T16:32:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexis\_Kurova](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/alexis_kurova/32/330862_2.png) [@Alexis\_Kurova](https://forums.eveonline.com/u/Alexis_Kurova)\
**Post date:** [August 8, 2022, 4:32pm UTC](https://forums.eveonline.com/t/python-esi-401-error-when-accessing-esi/372905/1 "2022-08-08T16:32:30Z")

</div>

Hi There,  
Hoping I could get some help with authentication. I’m pretty new to coding. As a bit of background:

- I copied [this approach](https://www.fuzzwork.co.uk/2017/03/14/using-esi-google-sheets/) (postman to get the refresh token) in Google sheets and still works today
- I’m trying to build out further and I am trying to recreate the request in python (below)
- I’m following the web app instructions, this is being built with Flask

```auto
def get_access_token():
    url = 'https://login.eveonline.com/v2/oauth/token'
    call_time = datetime.datetime.now()
    response = requests.post(
        url,
        params={"grant_type":"refresh_token", 'refresh_token': os.environ['REFRESH_TOKEN']},
        headers={'Content-Type': 'application/x-www-form-urlencoded', 
                'Authorization': 'Basic '+ os.environ['CLIENT_ID']+':'+os.environ['CLIENT_SECRET']}
    )
# response.statuscode == 401 at this point
# below is moot, we dont get that far, I'm including it to show that 
# I've been trying to update 'REFRESH_TOKEN' if they send
# a different one back

    os.environ['TOKEN_EXPIRY'] = call_time + datetime.timedelta(seconds=response['expires_in'])
    os.environ['REFRESH_TOKEN'] = response['refresh_token']

    return response['access_token']
# --> TypeError: 'Response' object is not subscriptable

```

Excuse the abuse of `os.environ` for now please…

Anyone able to spot where I’m going wrong? Or is there something I can do to get a more descriptive error?

I have tried:

- Adding and removing the `'Host':'login.eveonline.com'` header
- Removing the `params` parameter and constructing the url
- hard-coding the keys/IDs instead of using system variables

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/blacksmoke16/32/376589_2.png) [@Blacksmoke16](https://forums.eveonline.com/u/Blacksmoke16)\
**Post date:** [August 8, 2022, 4:56pm UTC](https://forums.eveonline.com/t/python-esi-401-error-when-accessing-esi/372905/2 "2022-08-08T16:56:57Z")

</div>

Can you also share the response body you get back when it 401s? That will probably have some information in it that will narrow down the problem.

EDIT: You also need to base64 encode your client id and client secret as part of the `Basic` auth header. See [Refreshing tokens | esi-docs](https://docs.esi.evetech.net/docs/sso/refreshing_access_tokens.html). So that’s probably your problem.

---

<div class="post-metadata">

**Author:** ![Alexis\_Kurova](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/alexis_kurova/32/330862_2.png) [@Alexis\_Kurova](https://forums.eveonline.com/u/Alexis_Kurova)\
**Post date:** [August 9, 2022, 10:12am UTC](https://forums.eveonline.com/t/python-esi-401-error-when-accessing-esi/372905/3 "2022-08-09T10:12:42Z")

</div>

Thanks for taking a look. I’ve refactored slightly to get the B64 encoding:

```auto
def get_access_token():
    url = 'https://login.eveonline.com/v2/oauth/token'
    call_time = datetime.datetime.now()
    client_id = os.environ['CLIENT_ID']
    client_secret = os.environ['CLIENT_SECRET']
    response = requests.post(
        url,
        params={"grant_type":"refresh_token", 'refresh_token': os.environ['REFRESH_TOKEN']},
        headers={'Content-Type': 'application/x-www-form-urlencoded', 
                'Host':'login.eveonline.com'},
        auth=HTTPBasicAuth(client_id, client_secret))
    return response.content

# --> b'{"error":"invalid_grant","error_description":"Grant type is not supported."}'

```

I’ve also done a longer version which generates the auth string and then encodes in b64 and adds to the header, instead of using `auth=HTTPBasicAuth(client_id, client_secret))`, which throws the same error.

---

<div class="post-metadata">

**Author:** ![Alexis\_Kurova](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/alexis_kurova/32/330862_2.png) [@Alexis\_Kurova](https://forums.eveonline.com/u/Alexis_Kurova)\
**Post date:** [August 9, 2022, 10:17am UTC](https://forums.eveonline.com/t/python-esi-401-error-when-accessing-esi/372905/4 "2022-08-09T10:17:49Z")

</div>

Aha!

Swapped `params` to `data` in the `requests.post()` method and it worked (along with the b64 change). Thank you so much!

Edit: final version for anyone looking in the future:

```auto
def get_access_token():
    url = 'https://login.eveonline.com/v2/oauth/token'
    call_time = datetime.datetime.now()
    client_id = os.environ['CLIENT_ID']
    client_secret = os.environ['CLIENT_SECRET']
    response = requests.post(
        url,
        data={'grant_type':'refresh_token', 'refresh_token': os.environ['REFRESH_TOKEN']},
        headers={'Content-Type': 'application/x-www-form-urlencoded', 
                'Host':'login.eveonline.com'},
        auth=HTTPBasicAuth(client_id, client_secret))
    return response.content

```

This returns a JSON exactly as per the docs:

```auto
{
  "access_token":"MXP...tg2",
  "token_type":"Bearer",
  "expires_in":1200,
  "refresh_token":"gEy...fM0"
}

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/eveonline/original/3X/4/1/41ec4921cbaca7a6f9ea556e7cc20018461eae5e.png) [@system](https://forums.eveonline.com/u/system)\
**Post date:** [November 7, 2022, 10:17am UTC](https://forums.eveonline.com/t/python-esi-401-error-when-accessing-esi/372905/5 "2022-11-07T10:17:58Z")

</div>

Closed on November 7, 2022, 10:17am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
