Flutter集成spotify_sdk遇异常:已装Spotify却提示未安装致AccessToken获取失败
问题排查与解决方案
1. 修复代码中的核心错误
你的代码存在致命逻辑问题:getAccessToken回调返回的value是实际访问令牌,但你未将其赋值给accessToken变量,导致后续connectToSpotifyRemote传入的是空字符串,直接引发连接失败。修正后的代码如下:
var accessToken = ""; SpotifySdk.getAccessToken( clientId: "546624bfa7964f959b54e9da5e3dca0f", redirectUrl: "https://critica.app/", scope: "app-remote-control") .then((value) { accessToken = value; // 关键:将获取到的令牌赋值给变量 print("ACCESS TOKEN: $accessToken"); SpotifySdk.connectToSpotifyRemote( clientId: "546624bfa7964f959b54e9da5e3dca0f", redirectUrl: "https://critica.app/", accessToken: accessToken) .then((connected) { print(connected ? "Spotify is connected" : "Spotify is not connected"); }); });
2. 验证iOS端关键配置
即使你按文档完成配置,也请重新核对以下核心项:
- LSApplicationQueriesSchemes配置:在
Info.plist中必须将spotify加入该数组,否则iOS会阻止应用检测Spotify是否安装。配置示例:<key>LSApplicationQueriesSchemes</key> <array> <string>spotify</string> </array> - Universal Links配置:由于你的重定向URL是HTTPS类型,需确保:
- Spotify开发者后台的应用设置中,已将
https://critica.app/添加到重定向URI列表 - iOS项目
Info.plist中开启Associated Domains,并添加applinks:critica.app - 你的服务器已部署正确的
apple-app-site-association文件
- Spotify开发者后台的应用设置中,已将
- 重定向URI一致性:代码中的
redirectUrl必须与Spotify后台配置的完全一致,包括大小写、斜杠等细节。
3. 额外排查方向
- 升级Spotify版本:确保设备上的Spotify是最新版,旧版本可能存在兼容性问题,无法被
spotify_sdk正确识别 - 验证测试用户状态:在Spotify开发者后台确认你的账号确实在
Users测试列表中,且应用处于Development模式 - 先检测安装状态:在调用
getAccessToken前,先执行以下代码确认应用能否识别Spotify:SpotifySdk.isSpotifyInstalled().then((isInstalled) { print("Spotify installed: $isInstalled"); });
内容的提问来源于stack exchange,提问作者Muse Gimikael
相关产品推荐
相关产品推荐

