NewToken will create a new OAuth2 token request. The user will be prompted to visit a URL, and after access is granted a new OAuth2 token is returned. An error is returned if encountered.
(ds DiskplayerServer)
| 35 | // The user will be prompted to visit a URL, and after access is granted a new OAuth2 token is returned. |
| 36 | // An error is returned if encountered. |
| 37 | func NewToken(ds DiskplayerServer) (*oauth2.Token, error) { |
| 38 | s, err := ds.RunCallbackServer() |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | |
| 43 | u := ds.Authenticator().AuthURL(STATE_IDENTIFIER) |
| 44 | fmt.Println("Please log in to Spotify by visiting the following page in your browser:", u) |
| 45 | |
| 46 | t := <-ds.TokenChannel() |
| 47 | |
| 48 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 49 | defer cancel() |
| 50 | err = s.Shutdown(ctx) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | |
| 55 | return t, nil |
| 56 | } |
| 57 | |
| 58 | // ReadToken will attempt to deserialize a token whose path is defined in the diskplayer.yaml |
| 59 | // configuration file under the token.file_path field. |