You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何自动获取Spotify授权令牌实现AppleScript添加曲目到私有播放列表?

解决Spotify AppleScript自动获取授权令牌的问题

我明白你想要实现的是不用手动更新Bearer ID,让AppleScript自动获取有效令牌来把当前播放曲目添加到私有播放列表。你遇到的invalid_client错误其实是授权流程里的常见问题,咱们一步步来理清。

错误原因分析

你现在尝试的授权码流程里有几个关键错误:

  • 你把过期的Bearer ID当成了code参数传递——这完全不对,code是用户完成授权后回调返回的授权码,不是之前的访问令牌。
  • 可能你的Base64编码有问题,比如包含了多余的换行符,或者Client ID/Secret输入错误。
  • 授权码流程需要先引导用户完成授权获取code,再用这个code换访问令牌,你跳过了前面的授权步骤直接请求令牌,自然会报错。

正确的解决方案:使用客户端凭据流(适合后台脚本)

因为你的需求是自己账号下的私有播放列表操作,不需要访问其他用户的数据,所以更适合用客户端凭据流,这个流程不需要用户手动授权,直接用Client ID和Secret获取令牌,步骤更简单:

步骤1:获取正确的Base64编码

首先确认你的Client ID和Client Secret没有写错,然后用终端生成正确的Base64编码(注意echo -n不能省略,否则会带多余换行符导致认证失败):

echo -n "你的ClientID:你的ClientSecret" | base64

步骤2:修改AppleScript自动获取令牌

替换你之前的授权部分,用下面的代码获取有效访问令牌:

-- 获取访问令牌
set clientID to "你的ClientID"
set clientSecret to "你的ClientSecret"
set base64Credentials to do shell script "echo -n '" & clientID & ":" & clientSecret & "' | base64"

set tokenURL to "https://accounts.spotify.com/api/token"
set curlCommand to "curl -X POST '" & tokenURL & "' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Authorization: Basic " & base64Credentials & "' -d 'grant_type=client_credentials'"

set tokenResponse to do shell script curlCommand
-- 方案1:用jq解析JSON(需提前安装jq:brew install jq)
set bearerID to do shell script "echo '" & tokenResponse & "' | jq -r '.access_token'"

-- 方案2:纯AppleScript文本解析(无需额外工具)
-- set startPos to offset of "\"access_token\":\"" in tokenResponse
-- set endPos to offset of "\"," in tokenResponse starting at startPos + 16
-- set bearerID to text (startPos + 16) thru (endPos - 1) of tokenResponse

步骤3:整合主逻辑使用自动令牌

把原来手动设置的BearerID替换成上面获取的bearerID,确保整个流程连贯。

完整的AppleScript示例

-- 配置信息
set userID to "你的用户ID"
set selectedPlaylistID to "你的目标播放列表ID"
set clientID to "你的ClientID"
set clientSecret to "你的ClientSecret"

-- 1. 获取当前播放曲目ID
tell application "Spotify"
    set currentSpotifyID to id of current track as string
end tell
set currentlyPlayingTrack to trim_line(currentSpotifyID, "spotify:track:", 0)

-- 2. 获取访问令牌
set base64Credentials to do shell script "echo -n '" & clientID & ":" & clientSecret & "' | base64"
set tokenURL to "https://accounts.spotify.com/api/token"
set curlTokenCommand to "curl -X POST '" & tokenURL & "' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Authorization: Basic " & base64Credentials & "' -d 'grant_type=client_credentials'"
set tokenResponse to do shell script curlTokenCommand
-- 用jq解析access_token(需提前安装jq)
set bearerID to do shell script "echo '" & tokenResponse & "' | jq -r '.access_token'"

-- 3. 添加曲目到播放列表
set addTrackURL to "https://api.spotify.com/v1/users/" & userID & "/playlists/" & selectedPlaylistID & "/tracks?uris=spotify%3Atrack%3A" & currentlyPlayingTrack
set curlAddCommand to "curl -X POST '" & addTrackURL & "' -H 'Accept: application/json' -H 'Authorization: Bearer " & bearerID & "'"
do shell script curlAddCommand

-- 修剪子程序
on trim_line(this_text, trim_chars, trim_indicator) -- 0 = 开头, 1 = 结尾, 2 = 两端
    set x to the length of the trim_chars
    -- 修剪开头
    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 -- 文本仅包含修剪字符
                return ""
            end try
        end repeat
    end if
    -- 修剪结尾
    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 -- 文本仅包含修剪字符
                return ""
            end try
        end repeat
    end if
    return this_text
end trim_line

重要注意事项

  • 权限设置:在Spotify开发者后台的应用设置里,必须启用playlist-modify-private权限,否则添加曲目时会提示权限不足。
  • 令牌有效期:客户端凭据流的令牌有效期是1小时,所以每次运行脚本时重新获取令牌是最优方案,无需缓存。
  • Client ID/Secret安全:不要把包含Client ID和Secret的脚本分享给他人,这相当于你的应用密钥,泄露后可能被滥用API配额。

内容的提问来源于stack exchange,提问作者Hugues

火山引擎 最新活动