Android Spotify SDK登录错误意外令牌

Posted

技术标签:

【中文标题】Android Spotify SDK登录错误意外令牌【英文标题】:Android Spotify SDK Login error Unexpected token 【发布时间】:2016-11-01 15:18:09 【问题描述】:

我正在尝试在 android 应用程序上实现 Spotify SDK,当我单击按钮时,Spotify 活动开始正常,但是当我尝试登录时没有任何反应,它记录了我一个 Unexpected token error,我使用了完全相同的代码就像在 Spotify 教程中一样,希望你能提供帮助

    06-29 13:40:36.677 4708-4708/com.app.project.silverbars I/chromium: [INFO:async_pixel_transfer_manager_android.cc(60)] Async pixel transfers not supported
06-29 13:40:36.789 4708-4708/com.app.project.silverbars I/chromium: [INFO:async_pixel_transfer_manager_android.cc(60)] Async pixel transfers not supported
06-29 13:40:37.505 4708-4708/com.app.project.silverbars D/dalvikvm: GC_FOR_ALLOC freed 1600K, 15% free 9975K/11700K, paused 6ms, total 6ms
06-29 13:41:02.605 4708-4708/com.app.project.silverbars I/chromium: [INFO:CONSOLE(6)] "SyntaxError: Unexpected token I
                                                                        at Object.parse (native)
                                                                        at Y (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:5:6501)
                                                                        at xt (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:6:14454)
                                                                        at https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:6:14923
                                                                        at i (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:5:1297)
                                                                        at Tt (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:6:14933)
                                                                        at o (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:6:15632)
                                                                        at s (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:7:2578)
                                                                        at https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:7:2750
                                                                        at f.$eval (https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js:7:10267)", source: https://d2d1dxiu3v1f2i.cloudfront.net/19b92cb/js/index.js (6)

Spotify活动:

public class SpotifyMusic extends AppCompatActivity implements
        PlayerNotificationCallback, ConnectionStateCallback 

    // TODO: Replace with your client ID
    private static final String CLIENT_ID = "b51d25ed8e514fa5927c028d5827a358";
    // TODO: Replace with your redirect URI
    private static final String REDIRECT_URI = "yourcustomprotocol://callback";

    // Request code that will be passed together with authentication result to the onAuthenticationResult callback
    // Can be any integer
    private static final int REQUEST_CODE = 1337;

    private Player mPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spotify_music);

        AuthenticationRequest.Builder builder =
                new AuthenticationRequest.Builder(CLIENT_ID, AuthenticationResponse.Type.TOKEN, REDIRECT_URI);
        builder.setScopes(new String[]"user-read-private", "streaming");
        AuthenticationRequest request = builder.build();

        AuthenticationClient.openLoginActivity(this, REQUEST_CODE, request);
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
        super.onActivityResult(requestCode, resultCode, intent);

        // Check if result comes from the correct activity
        if (requestCode == REQUEST_CODE) 
            AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
            if (response.getType() == AuthenticationResponse.Type.TOKEN) 
                Config playerConfig = new Config(this, response.getAccessToken(), CLIENT_ID);
                mPlayer = Spotify.getPlayer(playerConfig, this, new Player.InitializationObserver() 
                    @Override
                    public void onInitialized(Player player) 
                        mPlayer.addConnectionStateCallback(SpotifyMusic.this);
                        mPlayer.addPlayerNotificationCallback(SpotifyMusic.this);
                        mPlayer.play("spotify:track:2TpxZ7JUBn3uw46aR7qd6V");
                    

                    @Override
                    public void onError(Throwable throwable) 
                        Log.e("MainActivity", "Could not initialize player: " + throwable.getMessage());
                    
                );
            
        
    

    @Override
    public void onLoggedIn() 
        Log.d("MainActivity", "User logged in");
    

    @Override
    public void onLoggedOut() 
        Log.d("MainActivity", "User logged out");
    

    @Override
    public void onLoginFailed(Throwable error) 
        Log.d("MainActivity", "Login failed");
    

    @Override
    public void onTemporaryError() 
        Log.d("MainActivity", "Temporary error occurred");
    

    @Override
    public void onConnectionMessage(String message) 
        Log.d("MainActivity", "Received connection message: " + message);
    

    @Override
    public void onPlaybackEvent(EventType eventType, PlayerState playerState) 
        Log.d("MainActivity", "Playback event received: " + eventType.name());
        switch (eventType) 
            // Handle event type as necessary
            default:
                break;
        
    

    @Override
    public void onPlaybackError(ErrorType errorType, String errorDetails) 
        Log.d("MainActivity", "Playback error received: " + errorType.name());
        switch (errorType) 
            // Handle error type as necessary
            default:
                break;
        
    

    @Override
    protected void onDestroy() 
        // VERY IMPORTANT! This must always be called or else you will leak resources
        Spotify.destroyPlayer(this);
        super.onDestroy();
    

【问题讨论】:

您的 redirect_uri 应该与我在 Spotify 开发者控制台上的应用下提到的 redirect_uri 匹配。 【参考方案1】:

您得到的实际上是JSON.parse() 处的 JSON 错误

如果要解析的字符串不是有效的 JSON,则抛出 SyntaxError 异常。

查看您的代码后,我可以看到您没有完全按照教程进行操作。有说明

MainActivity.java 的完整代码现在应该如下所示,但使用您自己的客户端 ID 和重定向 URI

因为你的redirect URI 没有设置

private static final String REDIRECT_URI = "yourcustomprotocol://callback";

您的 URI 无法解析。这与您的错误并非巧合,因为在登录过程中使用了重定向 URI,并且基本上是 在成功的帐户授权后客户端将发送到的位置

现在关于如何设置它,由于我没有使用过 Spotify SDK,所以我帮不了你太多,但你可以使用类似的东西

private static final String REDIRECT_URI = "http://localhost:PORT_NUMBER://authenticationResponse"

PORT_NUMBER 可以是大于 1024 的任何值

有关更多信息,此问题可能会有所帮助:Spotify redirect URI。

希望你能解决这个问题。

【讨论】:

我真的不明白“REDIRECT_URI”,为什么我需要一个本地主机来解析 json?我是否必须创建另一个文件来解析重定向 uri?

以上是关于Android Spotify SDK登录错误意外令牌的主要内容,如果未能解决你的问题,请参考以下文章

使用android sdk登录spotify时如何获取帐户类型

Spotify 登录 Spotify iOS sdk beta 版本 3

如何在 Spotify SDK for Android 上刷新访问令牌?

Spotify ios SDK 登录:无效的重定向 URI

Flutter:如何将原生 SDK(Spotify Android SDK)添加到应用程序中?

Spotify IOS SDK Beta 结束播放供应商。 Code=4 传递了一个意外的 NULL 指针