-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85af59e
commit 25f039c
Showing
6 changed files
with
472 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package spotifyappauth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
spotifyauth "github.com/zmb3/spotify/v2/auth" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/zmb3/spotify/v2" | ||
) | ||
|
||
// redirectURI is the OAuth redirect URI for the application. | ||
// You must register an application at Spotify's developer portal | ||
// and enter this value. | ||
const redirectURI = "http://localhost:8080/callback" | ||
|
||
var ( | ||
auth = spotifyauth.New(spotifyauth.WithRedirectURL(redirectURI), spotifyauth.WithScopes(spotifyauth.ScopePlaylistModifyPublic)) | ||
ch = make(chan *spotify.Client) | ||
state = "abc123" | ||
) | ||
|
||
func AuthUser() { | ||
err := godotenv.Load() | ||
// first start an HTTP server | ||
http.HandleFunc("/callback", completeAuth) | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
log.Println("Got request for:", r.URL.String()) | ||
}) | ||
go func() { | ||
err := http.ListenAndServe(":8080", nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
url := auth.AuthURL(state) | ||
splitUrl := strings.Split(url, "client_id=") | ||
clientID := "client_id=" + string(os.Getenv("SPOTIFY_ID")) | ||
|
||
fmt.Println(os.Getenv("SPOTIFY_ID")) | ||
|
||
tempurl := splitUrl[0] + clientID + splitUrl[1] | ||
|
||
fmt.Println(tempurl) | ||
|
||
fmt.Println("Please log in to Spotify by visiting the following page in your browser:", tempurl) | ||
|
||
// wait for auth to complete | ||
client := <-ch | ||
|
||
// use the client to make calls that require authorization | ||
user, err := client.CurrentUser(context.Background()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("You are logged in as:", user.ID) | ||
} | ||
|
||
func completeAuth(w http.ResponseWriter, r *http.Request) { | ||
tok, err := auth.Token(r.Context(), state, r) | ||
if err != nil { | ||
http.Error(w, "Couldn't get token", http.StatusForbidden) | ||
log.Fatal(err) | ||
} | ||
if st := r.FormValue("state"); st != state { | ||
http.NotFound(w, r) | ||
log.Fatalf("State mismatch: %s != %s\n", st, state) | ||
} | ||
|
||
// use the token to get an authenticated client | ||
client := spotify.New(auth.Client(r.Context(), tok)) | ||
fmt.Fprintf(w, "Login Completed!") | ||
ch <- client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.