我如何从Go中的spotify Web api访问访问令牌?

问题描述 投票:0回答:1

我需要从 CompleteAuth 函数访问我的访问令牌,以便我可以在其他端点中使用。



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

我尝试创建一个指向 tok 变量的指针,但我似乎不起作用。 是否可以创建一个包含由completeAuth 函数生成的访问令牌的自定义界面。

api go pointers spotify go-fiber
1个回答
0
投票

您可以创建自定义接口来存储 CompleteAuth 函数生成的访问令牌,然后在其他端点中使用该接口。以下是如何执行此操作的示例:

package main

import (
    "context"
    "fmt"
    "log"
    "net/http"

    "github.com/zmb3/spotify"
)

// AccessTokenProvider defines the interface for accessing the access token.
type AccessTokenProvider interface {
    AccessToken() string
}

// TokenHolder implements the AccessTokenProvider interface.
type TokenHolder struct {
    Token *spotify.Token
}

// AccessToken returns the access token.
func (th *TokenHolder) AccessToken() string {
    return th.Token.AccessToken
}

var accessTokenProvider AccessTokenProvider

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

    // Set the access token provider
    accessTokenProvider = &TokenHolder{Token: tok}

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

    // Now you can use the access token provider to access the token
    accessToken := accessTokenProvider.AccessToken()
    fmt.Println("Access Token from RegHand:", accessToken)
}

func main() {
    // Your main function implementation here
}

我使用返回访问令牌的方法 AccessToken() 创建了一个 AccessTokenProvider 接口。

然后,定义一个实现该接口的TokenHolder结构体,并保存CompleteAuth函数生成的访问令牌。

最后,当在 CompleteAuth 函数中生成访问令牌时,我将 accessTokenProvider 变量设置为指向 TokenHolder 的实例。

然后,您可以使用 accessTokenProvider 访问 RegHand 函数或代码的任何其他部分中的访问令牌。

例如:

func SomeOtherFunction() {
    // Access the access token using the accessTokenProvider
    accessToken := accessTokenProvider.AccessToken()
    fmt.Println("Access Token from SomeOtherFunction:", accessToken)
}
© www.soinside.com 2019 - 2024. All rights reserved.