首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spotify授权

Spotify授权
EN

Stack Overflow用户
提问于 2018-01-23 13:52:07
回答 1查看 1.2K关注 0票数 0

我试图设置一个简单的快捷方式,将当前播放的Spotify曲目添加到spotify播放列表中,以记录我在流中听到的歌曲。

我在用applescript做这个。

到目前为止,我发现有些Spotify API应该能满足我的需求,但我在最后一步失败了。

更确切地说,由于我想修改私有播放列表,这个api (1):https://developer.spotify.com/web-api/console/post-playlist-tracks/需要一个授权令牌。此令牌可以手动检索,到目前为止,此Applescript正在工作:

代码语言:javascript
复制
-- Main

set userID to "XXXXX"
-- my real userID can be retrieved with https://developer.spotify.com/web-api/console/get-current-user/#complete

set SelectedPlaylistID to "YYYY"
-- my target playlist can be retrieved with https://developer.spotify.com/web-api/console/get-playlists/#complete

set BearerID to "ZZZZ"
-- ZZZZ can be retrieved manually on page (1) 

tell application "Spotify"
  set currentSpotifyID to id of current track as string
end tell

set currentlyPlayingTrack to trim_line(currentSpotifyID, "spotify:track:", 0)
set theURL to "'https://api.spotify.com/v1/users/" & userID & "/playlists/" & SelectedPlaylistID & "/tracks?uris=spotify%3Atrack%3A" & currentlyPlayingTrack & "' -H 'Accept: application/json' -H 'Authorization: Bearer " & BearerID & "'"
-- built from https://developer.spotify.com/web-api/console/post-playlist-tracks/#complete

set theCommand to "curl -X POST " & theURL
do shell script theCommand


-- trim subroutine; not the subject here; mentioned for completeness, you can skip it

on trim_line(this_text, trim_chars, trim_indicator)
-- 0 = beginning, 1 = end, 2 = both
set x to the length of the trim_chars
-- TRIM BEGINNING
if the trim_indicator is in {0, 2} then
    repeat while this_text begins with the trim_chars
        try
            set this_text to characters (x + 1) thru -1 of this_text as string
        on error
            -- the text contains nothing but the trim characters
            return ""
        end try
    end repeat
end if
-- TRIM ENDING
if the trim_indicator is in {1, 2} then
    repeat while this_text ends with the trim_chars
        try
            set this_text to characters 1 thru -(x + 1) of this_text as string
        on error
            -- the text contains nothing but the trim characters
            return ""
        end try
    end repeat
end if
return this_text
end trim_line

现在出现了棘手的部分。

BearerID是临时的,所以每次我都需要手动检索它,这违背了快捷方式的目的。

我想自动检索"ZZZZ",BearerID。

有一个关于授权线程的教程,坦白地说,我无法解决。

我认为我有一个在授权代码流中实现请求4)的解决方案:

代码语言:javascript
复制
set base64encodedCredentials to "WWWW"
-- where WWWW is the result of 
-- $ echo - n 'CCCC:SSSS' | base64
-- "CCCC" being my Spotify ClientID and "SSSS" my Client Secret as given on https://beta.developer.spotify.com/dashboard/applications/

set BearerID to "ZZZZ"
-- where ZZZZ is the expired token

set authorizationURL to "-H 'Authorization: Basic " & base64encodedCredentials & "' -d grant_type=authorization_code -d code=" & BearerID & "-d redirect_uri=https%3A%2F%2Fwww.foo.com%2Fauth https://accounts.spotify.com/api/token"

set theAuthorization to "curl -X 'POST' " & authorizationURL

do shell script theAuthorization   

但我犯了个错误

“{\”错误\“:\”invalid_client\“,\”error_description\“:\”无效客户端\“}”

因此,基本上我还没有找到在Spotify中自动检索授权令牌的正确方法。

知道吗?

EN

回答 1

Stack Overflow用户

发布于 2018-01-23 16:15:47

看起来您使用的是部分授权代码流和部分客户端凭据流。

如果您想使用客户端凭据(这将允许您获取通用Spotify数据,而不是特定于用户的数据),则需要将grant_type设置为client_credentials并删除重定向uri。将这一行改为:

代码语言:javascript
复制
set authorizationURL to "-H 'Authorization: Basic " & base64encodedCredentials & "' -d grant_type=client_credentials https://accounts.spotify.com/api/token"

如果您想使用授权代码(这将允许您获取特定于用户的数据),您需要遵循一个更复杂的流程。需要注意的是,这个流程需要用户交互来授予应用程序所请求的作用域。从脚本中很难做到这一点,但是下面是bash中的一个例子:https://gist.github.com/hughrawlinson/358afa57a04c8c0f1ce4f1fd86604a73

幸运的是,您只需要获得一次用户操作,因为一旦您有了刷新令牌,您就可以在后台刷新用户的访问令牌(授权指南中的步骤7)。

当前的方法无法工作的原因是,您所指的BearerID实际上是一个访问令牌,但是您正在将它用作该流程的“代码”(您提到的授权指南中的步骤4)。

我建议为此设置一个小型服务器,它可以在后台运行并处理身份验证和刷新。参见这里的示例,其中包含PythonNode.js中的授权代码流。

如果你有什么问题请告诉我!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48403386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档