# How to pass in authorization token to API and return contents?

**URL:** <https://forum.anaconda.com/t/how-to-pass-in-authorization-token-to-api-and-return-contents/49930>\
**Category:** PyScript\
**Created:** [January 31, 2023, 1:42am UTC](https://forum.anaconda.com/t/how-to-pass-in-authorization-token-to-api-and-return-contents/49930 "2023-01-31T01:42:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhinavsingh192](https://avatars.discourse-cdn.com/v4/letter/a/00a5b5/32.png) [@abhinavsingh192](https://forum.anaconda.com/u/abhinavsingh192)\
**Post date:** [January 31, 2023, 1:42am UTC](https://forum.anaconda.com/t/how-to-pass-in-authorization-token-to-api-and-return-contents/49930/1 "2023-01-31T01:42:50Z")

</div>

Apologies for the noob question. I’m creating a website on a static server (Github Pages) and am trying to call the Spotify API to obtain my most recently played track. I think the issue I’m having is that I’m having trouble entering the authorization token

This is the code I tried - look at the ‘response = await’ line:

```auto
<py-script>
      from pyodide.http import pyfetch
      import json
      import asyncio
      import re
      response = await pyfetch(url="https://api.spotify.com/v1/me/player/currently-playing", headers = {'authorization': --insert auth token here--} , method="GET")
      cal= str('C')
      output = f'{await response.json()}'
      def jj(output):

        str1 = output.replace("\'", "\"")
        st= json.loads(str1)

        return st
      
      pyscript.write(jj(output))

          </py-script>

```

I end up getting a ‘JsException(PythonError: Traceback (most recent call last): File “/lib/python3.10/asyncio/futures.py”, line 201, in result raise self.\_exception File “/lib/python3.10/asyncio/tasks.py”, line 232, in \_\_step result = coro.send(None) File “/lib/python3.10/site-packages/\_pyodide/\_base.py”, line 500, in eval\_code\_async await CodeRunner( File “/lib/python3.10/site-packages/\_pyodide/\_base.py”, line 353, in run\_async await coroutine File “”, line 7, in File “/lib/python3.10/site-packages/pyodide/http.py”, line 139, in json self.\_raise\_if\_failed() File “/lib/python3.10/site-packages/pyodide/http.py”, line 107, in \_raise\_if\_failed raise OSError( OSError: Request for [https://api.spotify.com/v1/me/player/currently-playing](https://api.spotify.com/v1/me/player/currently-playing) failed with status 400: )’

How can I fix this

---

<div class="post-metadata">

**Author:** ![JeffGlass](https://dub1.discourse-cdn.com/anaconda/user_avatar/forum.anaconda.com/jeffglass/32/1301_2.png) [@JeffGlass](https://forum.anaconda.com/u/JeffGlass)\
**Post date:** [January 31, 2023, 4:17pm UTC](https://forum.anaconda.com/t/how-to-pass-in-authorization-token-to-api-and-return-contents/49930/2 "2023-01-31T16:17:27Z")

</div>

Apologies for the duplicate answer - I just responded to you on Stack Overflow as well.

What version of PyScript are you using? Using top-level await like that was removed in Version 2022.12.1. The answer to your question will depend on which version of PyScript (and therefore which version of Pyodide) you are using.

My first impression is that you’ll want to wrap your `headers` object in `to_js`, as follows:

```auto
from pyodide.ffi import to_js
from js import Object

response = await pyfetch(
    url="https://api.spotify.com/v1/me/player/currently-playing",
    headers = to_js({'authorization': --insert auth token here--}, dict_converter=Object.fromEntries), 
    method="GET"
    )

```

This will cause the headers object to be passed as JavaScript object, as opposed to a Mapping or PyProxy as is likely currently happening.

---

<div class="post-metadata">

**Author:** ![abhinavsingh192](https://avatars.discourse-cdn.com/v4/letter/a/00a5b5/32.png) [@abhinavsingh192](https://forum.anaconda.com/u/abhinavsingh192)\
**Post date:** [January 31, 2023, 8:45pm UTC](https://forum.anaconda.com/t/how-to-pass-in-authorization-token-to-api-and-return-contents/49930/3 "2023-01-31T20:45:09Z")

</div>

Hi thank you for response. I am on version 0.20.0.

When I try to run your code, I get the following error:

> ModuleNotFoundError: No module named ‘pyodide.ffi’ )

---

<div class="post-metadata">

**Author:** ![JeffGlass](https://dub1.discourse-cdn.com/anaconda/user_avatar/forum.anaconda.com/jeffglass/32/1301_2.png) [@JeffGlass](https://forum.anaconda.com/u/JeffGlass)\
**Post date:** [January 31, 2023, 9:04pm UTC](https://forum.anaconda.com/t/how-to-pass-in-authorization-token-to-api-and-return-contents/49930/4 "2023-01-31T21:04:51Z")

</div>

Ah gotcha, so probably PyScript Alpha then? Is your link pointing at `https://pyscript.net/alpha/pyscript.js`? That’s a somewhat outdated version, the latest link is `https://pyscript.net/releases/2022.12.1/pyscript.js`.

If you do want to use the alpha version, try replacing that line that doesn’t work with `from pyodide import to_js`.
