【问题标题】:Spotify authorizationSpotify 授权
【发布时间】:2018-01-23 13:52:07
【问题描述】:

我正在尝试设置一个简单的快捷方式,将当前正在播放的 Spotify 曲目添加到 Spotify 播放列表中,以记录我在流中听到的那些我喜欢的曲目。

我正在使用applescript。

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

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

-- 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。

有一个关于授权的教程 https://developer.spotify.com/web-api/authorization-guide/#disqus_thread 坦率地说,我无法解决。

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

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   

但我得到一个错误

"{\"error\":\"invalid_client\",\"error_description\":\"无效客户端\"}"

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

有什么想法吗?

【问题讨论】:

    标签: applescript authorization spotify playlist


    【解决方案1】:

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

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

    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 中查看授权代码流示例。

    如果您有任何问题,请告诉我!

    【讨论】:

    • 感谢您的回答。
    • 我首先编写了一个客户端凭据流,然后才意识到它无法修改私人播放列表,所以我求助于授权代码。坦率地说,我发现设置一个永久服务器只是为了拥有一个本地快捷方式是非常过分的,除了我不知道该怎么做。我的想法是手动初始化数据,只使用脚本刷新令牌。我现在明白了,我一开始就无法获得代码。所以我担心我在这里不合群
    猜你喜欢
    • 2018-05-21
    • 1970-01-01
    • 2021-07-04
    • 2017-12-29
    • 2018-08-12
    • 2017-12-23
    • 2019-08-14
    • 1970-01-01
    • 2020-10-06
    相关资源
    最近更新 更多