Spotify Auth 库在登录时返回 Type.EMPTY

Den*_*yes 3 authentication android spotify retrofit spotify-app

我遵循了 android 身份验证指南和快速入门指南中的所有内容。我按照指南告诉我生成一个 SHA1 并将其添加到 Spotify 应用程序仪表板,我得到了 app_client 并将其添加到我的应用程序中。这两种情况仍然返回相同的东西。我什至尝试实现通过浏览器登录功能,但它仍然返回类型 EMPTY。

这是我的登录类

class SignInActivity : AppCompatActivity(), ConnectionStateCallback, Player.NotificationCallback {

private var mPlayer : SpotifyPlayer? = null

private val CLIENT_ID = //replace this
private val REDIRECT_URI = //replace this
private val REQUEST_CODE = 1337


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)

    btnSignin.setOnClickListener {
        val builder = AuthenticationRequest.Builder(CLIENT_ID, AuthenticationResponse.Type.TOKEN, REDIRECT_URI)
        builder.setScopes(arrayOf("user-read-private", "streaming"))
        val request = builder.build()

        AuthenticationClient.openLoginActivity(this, REQUEST_CODE, request)
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, intent)

    // Check if result comes from the correct activity
    if (requestCode == REQUEST_CODE) {
        val response = AuthenticationClient.getResponse(resultCode, intent)

        Log.i("LoginActivity", Gson().toJson(response))

        when (response.type) {
        // Response was successful and contains auth token
            AuthenticationResponse.Type.TOKEN -> {
                Log.i("LoginActivity", "is equals to TOKEN")

                val playerConfig = Config(this, response.accessToken, CLIENT_ID)
                Spotify.getPlayer(playerConfig, this, object : SpotifyPlayer.InitializationObserver {
                    override fun onInitialized(spotifyPlayer: SpotifyPlayer) {
                        mPlayer = spotifyPlayer
                        mPlayer!!.addConnectionStateCallback(this@SignInActivity)
                        mPlayer!!.addNotificationCallback(this@SignInActivity)
                    }

                    override fun onError(throwable: Throwable) {
                        Log.e("LoginActivity", "Could not initialize player: " + throwable.message)
                    }
                })
            }

        // Auth flow returned an error
            AuthenticationResponse.Type.ERROR -> {
                Log.i("LoginActivity", "ERROR!: $response.error")
            }

            AuthenticationResponse.Type.EMPTY -> {
                Log.i("LoginActivity", "EMPTY!")
            }
        }
    }
}

override fun onLoggedIn() {
    Log.d("LoginActivity", "User logged in")

    // This is the line that plays a song.
    mPlayer!!.playUri(null, "spotify:track:2TpxZ7JUBn3uw46aR7qd6V", 0, 0)
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的 AndroidManifest 文件`

<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SignInActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <data
                android:host="callback"
                android:scheme="backdrop.io"/>
        </intent-filter>
    </activity>
    <activity
        android:name="com.spotify.sdk.android.authentication.LoginActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
</application>
Run Code Online (Sandbox Code Playgroud)

我正在尝试从中获取 Type.TOKEN

小智 9

我自己也遇到了完全相同的问题,问题出在以下行中onActivityResult():

val response = AuthenticationClient.getResponse(resultCode, intent)
Run Code Online (Sandbox Code Playgroud)

data应该作为第二个参数而不是intent.

这不是编译器错误的原因是 Kotlin 将 getter 和 setter 转换为属性,intent是对Activity方法的调用getIntent()。

  • 这就是我的情况的问题。不幸的是,Spotify 的示例代码返回的“Intent”名为“intent”。当您碰巧复制并粘贴示例并使用 Kotlin 时,编译器会将“intent”视为“getIntent()”。 (2认同)