1

I need to access my access token from the CompleteAuth function, so i can use in my other endpoints.



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.Println("Access Token:" + tok.AccessToken + "\n")
    fmt.Fprintf(w, "Login Completed!")
    ch <- client
}

func RegHand(w http.ResponseWriter, r *http.Request) {
    log.Println("Got request for:", r.URL.String())

    url := auth.AuthURL(state)
    fmt.Println("Please log in to Spotify by visiting the following page in your browser:", url+"\n")

    // 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)
}

I've triend to create a pointer to the tok variable, but i does not seem to work. Would it be possible to create a custom interface that contains the acces token that is generated by the completeAuth function.

0 Answers0