doqp87012 2019-07-18 20:45
浏览 64

Spotify Web API当前用户错误

I'm working on a hobby project in Go that uses the Spotify Web API. My first step was to get the Authorization Code OAuth flow working. This took a while to get right but now everything seems to work well and I can use my app to receive an access token.

My first test was to use the: https://api.spotify.com/v1/me endpoint to make sure that it returned my user_id as the current user. Unfortunately, it returns some other user (let's call him Bill Gates). I've revoked access to the app and used it to receive a new Access Token but it always returns Bill Gates as the current user.

I'm not logging into Spotify as Bill Gates so I'm not sure why he is being returned as the current user. Isn't the current user returned by the API endpoint supposed to be the user that logged in to receive an Access Token?

Here's my token requesting code.

package main

import (
    "bytes"
    "io"
    "log"
    "net/http"
    "net/url"
    "os"

    b64 "encoding/base64"
)

const (
    apiURL       = "https://accounts.spotify.com/api/token"
    redirectURI  = "https://XXXXXXXXXXXXXXXXXXXXXXXXX"
    clientID     = "XXXXXXXX"
    clientSecret = "XXXXXXXX"
)

func main() {
    code := "Passed into Lambda as a parameter"
    client := &http.Client{}

    data := url.Values{}
    data.Add("grant_type", "authorization_code")
    data.Add("code", code)
    data.Add("redirect_uri", redirectURI)

    req, err := http.NewRequest("POST", apiURL, bytes.NewBufferString(data.Encode()))
    if err != nil {
        log.Fatal("Couldn't create new POST request")
    }

    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    authString := "Basic " + b64.StdEncoding.EncodeToString([]byte(clientID+":"+clientSecret))
    req.Header.Set("Authorization", authString)

    resp, err := client.Do(req)
    if err != nil {
        log.Fatal("Couldn't 'Do' request")
    }
    defer resp.Body.Close()

    io.Copy(os.Stdout, resp.Body)
    // Body contains valid access and refresh tokens
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集