Skip to content

Commit

Permalink
Add refresh token endpoint for Spotify
Browse files Browse the repository at this point in the history
  • Loading branch information
zembrodt committed Nov 29, 2021
1 parent 4e464cd commit 01a37f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions controller/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"golang.org/x/oauth2"
"net/http"
)

Expand All @@ -20,6 +21,7 @@ var scopes = []string{

func (c *MusicAPIController) createAuthHandlers() {
c.handleFunc(pathToken, c.getAuthTokens, http.MethodPost)
c.handleFunc(pathToken, c.updateAuthToken, http.MethodPut)
}

func (c *MusicAPIController) getAuthTokens(w http.ResponseWriter, r *http.Request) {
Expand All @@ -45,3 +47,25 @@ func (c *MusicAPIController) getAuthTokens(w http.ResponseWriter, r *http.Reques
}
respondWithJSON(w, http.StatusOK, token)
}

func (c *MusicAPIController) updateAuthToken(w http.ResponseWriter, r *http.Request) {
// Get request parameters
code := r.FormValue(codeKey)

if len(code) == 0 {
respondWithError(w, http.StatusBadRequest, "Invalid %s", codeKey)
return
}

refreshToken := &oauth2.Token{
RefreshToken: code,
}

tokenSource := c.conf.TokenSource(context.Background(), refreshToken)
newToken, err := tokenSource.Token()
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Unable to refresh auth token: %s", err.Error())
return
}
respondWithJSON(w, http.StatusOK, newToken)
}
2 changes: 1 addition & 1 deletion properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package musicapi

const (
Name = "Music Display API"
Version = "0.1.0"
Version = "0.1.1"
APIRoot = "/api"
)

0 comments on commit 01a37f1

Please sign in to comment.