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

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:

<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 failed with status 400: )’

How can I fix this

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:

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.

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’ )

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.