zmb3 / spotify

A Go wrapper for the Spotify Web API
Apache License 2.0
1.37k stars 287 forks source link

Oauth Credential Storage #64

Closed rphillips closed 6 years ago

rphillips commented 6 years ago

Is there a way to save the oauth credentials to the filesystem, so they can be used on a subsequent run?

falzm commented 6 years ago

Good question, I was actually about to ask a similar one...

I'm trying to save the code returned from the Spotify callback request and provide it to the Exchange() method, but it doesn't seem to work: I get a 400 Bad Request ({"error":"invalid_grant","error_description":"Invalid authorization code"}) 😞

zmb3 commented 6 years ago

Don’t save the code, save the token. You can get the token via the clients Token() method.

See #26

lvthillo commented 4 years ago

@falzm @zmb3 I've the same issue when I try to use the Exchange method with the token (also tried client.Token to get the token and it matches with the value in the browser and it matches all println).

Set cookie:

...
client := auth.NewClient(tok)
fmt.Fprintf(c.Writer, "Login Completed!")
user, err := client.CurrentUser()
if err != nil {
    log.Fatal(err)
}
fmt.Println("You are logged in as:", user.ID)
c.SetCookie("gin_cookie", tok.AccessToken, 36000, "/", "localhost", false, true)

Use AccessToken from cookie in exchange:

cookie, err := c.Request.Cookie("gin_cookie")
if err != nil {
    panic(err.Error())
}
cv := cookie.Value
cv2, err := auth.Exchange(cv)
...

But I also get: _oauth2: cannot fetch token: 400 Bad Request Response: {"error":"invalid_grant","errordescription":"Invalid authorization code"}