Announcements

Help Wizard

Step 1

NEXT STEP

Only valid bearer authentication supported when trying to get access token

Only valid bearer authentication supported when trying to get access token

I'm a beginner so I'm sorry if this is a stupid question. I'm trying to get an access code using the authorization flow in golang. I am quite sure I am getting the User authorization correctly but then when I try and get the access token I get the error: Only valid bearer authentication supported"
this is my golang code to get the access token:

func request_access_token() (string, error) {
fmt.Println("User auth code")
fmt.Println(user_auth_token)
data := url.Values{}
data.Add("code", user_auth_token)
data.Add("redirect_uri", redirectURI)
data.Add("grant_type", "authorization_code")

req, err := http.NewRequest("POST", "https://accounts.spotify.com/api/token", strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("error creating request: %v", err)
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

fmt.Println("Auth header")
auth := base64.StdEncoding.EncodeToString([]byte(os.Getenv("CLIENT_ID") + ":" + os.Getenv("CLIENT_SECRET")))
fmt.Println(auth)
req.Header.Set("Authorization", "Basic "+auth)
// Send request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()

// Read response
var token SpotifyToken
if err := json.NewDecoder(resp.Body).Decode(&token); err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}

// Check if request was successful
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status code: %d",
resp.StatusCode)
}

return token.AccessToken, nil
}

Reply
1 Reply

Hi.  This error message indicates there's something wrong with your os.Getenv("CLIENT_ID") and/or os.Getenv("CLIENT_SECRET") values.  Maybe output those to confirm they match with the ones in your Developer Dashboard settings.

Suggested posts

Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

ModeratorStaff / Moderator/ 4 years ago  in Social & Random