GetRemote cannot pass Headers: Bad Request

With the new GetRemote I thought it would be possible to get a token of the Spotify API, but I don’t know how to get it to work :frowning:

This is my shortcode spotify.html so far:

{{/* spotify.html
  https://developer.spotify.com/documentation/web-api/reference/
  https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow

  spotify album 1KAg47NePhjsKC4Y8ZC9z3
  spotify show 1O9vyJwNvUcdq1d9vFblQw 0
  spotify playlist 37i9dQZF1DXc51TI5dx7RC
  spotify track 2TpxZ7JUBn3uw46aR7qd6V
*/}}
{{- $_time := "" -}}
{{- $market := "DE" -}}
{{- $type := index .Params 0 -}}
{{- $code := index .Params 1 -}}
{{- $time := index .Params 2 | default "" -}}
{{- $code_safejs := safeJS (md5 (printf "%s_%s" $type $code)) -}}

{{ $base64 := base64Encode (printf "%s:%s" (getenv "HUGO_SECRET_SPOTIFY_ID") (getenv "HUGO_SECRET_SPOTIFY_SECRET")) }}
{{ $base64_2 := printf "%s %s" "Basic" $base64 }}

<p>{{$base64_2}}</p>

{{ $postResponse := resources.GetRemote "https://accounts.spotify.com/api/token" (dict
    "method" "post"
    "body" `{"form": "grant_type=client_credentials"}`
    "headers" (dict
        "Content-Type" "application/x-www-form-urlencoded"
        "Authorization" $base64_2
    )
)}}

{{ $postResponse }}

This is the shortcode in use in a content/test.md:

{{< spotify album 1KAg47NePhjsKC4Y8ZC9z3 >}}

But instead of a proper Token I’m getting a Bad Request, and I can’t seem to figure out the right syntax. With curl it’s easy:

$ curl -X "POST" -H "Authorization: Basic xyz" -d 'grant_type=client_credentials' https://accounts.spotify.com/api/token
{"access_token":"xyz","token_type":"Bearer","expires_in":3600}

How can I “translate” the -d Flag to the body (?) in the shortcode? I also had a look at the go http package, but I’m not getting it :frowning:

1 Like

I am also seeing the Bad Request error.

In my case I was able to work around it by using getJSON (since I wanted to retrieve a JSON anyway).

It appears that for some reason passing headers to GetRemote does not currently work.

I meant to file a bug report but at the moment my time is very short.

EDIT
I amended the topic title, as this is not something about Spotify’s API.

Oh, ok :see_no_evil:

Then I’m very interested in how you got the token with getJSON. I tried this as well, but how do you use it for POST-Requests? I thought it’s only for GET-Requests?

Actually I used the example from the documentation:

Ok, thanks, but imho this is only for GET-Requests. So not useful to get a token first :slight_smile:

{{ $opts := dict
  "method" "post"
  "headers" (dict
    "Authorization" $base64_2
    "Content-Type" "application/x-www-form-urlencoded"
  )
  "body" "grant_type=client_credentials"
}}
{{ $postResponse := resources.GetRemote "https://accounts.spotify.com/api/token" $opts }}
2 Likes

I have no idea why it’s working now if you move stuff around and use variables, but thank you very much! I’ve got it working now :tada:

Hint: if you want to use the result of $postResponse for the next getJSON use transform.Unmarshal:

{{ $postResponse := resources.GetRemote "https://accounts.spotify.com/api/token" $opts | transform.Unmarshal }}
{{ $headers := dict "Authorization" (printf "Bearer %s" $postResponse.access_token) }}
{{ $json := getJSON "https://api.spotify.com/v1/" $type "s/" $code "?market=" $market $headers }}

You set the body to this:

{"form": "grant_type=client_credentials"}

I set the body to this:

grant_type=client_credentials

I think I tried this yesterday as well, but maybe I just needed to sleep :smiley: thanks again!

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