英文:
Golang - spotify : This request requires user authentication
问题
我正在编写一个机器人,它会接收音乐的名称和艺术家,并将其添加到选定的播放列表中,使用zmb3 Spotify库。除了将音乐添加到播放列表部分之外,一切正常;
机器人给我返回了这个错误:此请求需要用户身份验证。
但是Spotify令牌和Spotify密钥是有效的。
我进行了搜索,但没有找到任何解决方法。
除了Spotify令牌和Spotify密钥之外,还需要其他内容吗?
我该怎么办?
代码:
// 创建Spotify客户端连接和上下文以连接Spotify
client, ctx := api.SpotifyConnection(SPOTIFYTOKEN, SPOTIFYSECRET)
// 添加播放列表
playlist, err := client.GetPlaylist(ctx, PLAYLISTID)
if err != nil {
log.Fatal(err.Error())
}
/* 一些代码 */
if results.Tracks != nil {
items := results.Tracks.Tracks
musicID := items[0].ID.String()
cmd.AddMusic(client, ctx, playlist.ID.String(), musicID)
错误信息:
2021/12/26 11:06:25 此请求需要用户身份验证。
英文:
I'm writing a bot that takes the name of the music and the artist and adds it to the selected playlist with the zmb3 Spotify library. Everything works except the section add music to the playlist ;
the bot gives me this error: this request requires user authentication.
but Spotify token and Spotify secret are valid.
I searched, but I didn't get anything.
Is there anything more needed than Spotify Token and Spotify Secret?
What should I do?
Code :
// create spotify client connection and context to connect spotify
client, ctx := api.SpotifyConnection(SPOTIFYTOKEN, SPOTIFYSECRET)
// add playlist
playlist, err := client.GetPlaylist(ctx, PLAYLISTID)
if err != nil {
log.Fatal(err.Error())
}
/* SOME CODES */
if results.Tracks != nil {
items := results.Tracks.Tracks
musicID := items[0].ID.String()
cmd.AddMusic(client, ctx, playlist.ID.String(), musicID)
ERROR:
2021/12/26 11:06:25 This request requires user authentication.
答案1
得分: 2
这样看待一下——你能在未登录账户的情况下向播放列表添加一首歌吗?对于你的代码也是一样的。这不是通过TOKEN
和SECRET_KEY
进行API身份验证的问题,而是需要引入用户身份验证,这样当用户打开你的应用时,他们可以登录并将歌曲添加到自己的播放列表中。
在 zmb3 的 README 中有一个 Authentication 部分,应该会有所帮助:
> 你可以使用客户端凭据流进行身份验证,但这不提供访问用户私有数据的任何授权。对于大多数用例,你可能需要使用授权码流。这个包包含一个 Authenticator 类型来处理这些细节。
英文:
Look at it this way - can you add a track to your playlist while not logged into your account? The same goes for your code. It's not an issue of authenticating to the API with TOKEN
and SECRET_KEY
, but you need to introduce user authentication, so that when a user opens your app, they log in and can add tracks to their own playlists.
There is an Authentication section in the zmb3 README that should help:
> You can authenticate using a client credentials flow, but this does not provide any authorization to access a user's private data. For most use cases, you'll want to use the authorization code flow. This package includes an Authenticator type to handle the details for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论