You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android下AppAuth对接SingPass后浏览器未自动返回应用问题

对接SingPass授权后无法返回Android应用的问题

我正在对接SingPass实现授权登录,以下是实现授权流程的Kotlin代码:

private fun configAndStartActivity(){

    val jsonConfig = "{" +
            "\"issuer\":\"https://test.api.myinfo.gov.sg\"," +
            "\"authorizationEndpoint\":\"https://test.api.myinfo.gov.sg/com/v3/authorise\"," +
            "\"tokenEndpoint\":\"https://test.api.myinfo.gov.sg/com/v3/token\"" +
            "}"

    val serviceConfig = AuthorizationServiceConfiguration.fromJson(jsonConfig)

    val clientId = "MY_CLIENT_ID"
    val redirectUri = Uri.parse("https://our_redirectURL/testpath")
    val builder = AuthorizationRequest.Builder(
        serviceConfig,
        clientId,
        ResponseTypeValues.CODE,
        redirectUri
    )
    builder.setScope("openid")
    val additionalParams = mutableMapOf<String, String>()
    additionalParams["purpose"] = "resetting Login Password."
    additionalParams["attributes"] = "uinfin,name,hanyupinyinname,aliasname,hanyupinyinaliasname,marriedname,dob"
    additionalParams["redirect_uri_https_type"] = "app_claimed_https"
    builder.setAdditionalParameters(additionalParams)
    builder.setState("STATE_ID")
    val authRequest = builder.build()
    val authService = AuthorizationService(this)
    val authIntent = authService.getAuthorizationRequestIntent(authRequest)
    startActivityForResult(authIntent, 100)
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == RC_AUTH) {
        val resp = AuthorizationResponse.fromIntent(data!!)
        val ex = AuthorizationException.fromIntent(data)

        if (resp != null) {
            mAuthService = AuthorizationService(this)
            mStateManager?.updateAfterAuthorization(resp, ex)

            mAuthService?.performTokenRequest(
                resp.createTokenExchangeRequest()
            ) { resp, ex ->
                if (resp != null) {

                    mStateManager?.updateAfterTokenResponse(resp, ex)
                    Log.d("accessToken", resp.accessToken)
                } else {
                    // authorization failed, check ex for more details
                }
            }
        } 
    }
}

AndroidManifest.xml中的对应配置如下:

<activity
    android:name="net.openid.appauth.RedirectUriReceiverActivity"
    tools:node="replace"
    android:exported="true">

    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="https"
            android:host="our_redirectURL"
            android:path="/testpath/"/>
    </intent-filter>

    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="https"
            android:host="our_redirectURL"
            android:path="/testpathnew"/>
    </intent-filter>
</activity>

调用configAndStartActivity()方法后,浏览器可正常打开并完成SingPass登录流程,但登录后浏览器不会自动关闭,也无法跳转回应用。该流程在对接Google认证时可正常触发应用跳转。通过chrome://inspect/#devices查看,最终重定向的URL为:

https://our_redirectURL/testpath/#/prelogin/99/DEEPLINK/FRGTPWD?code=xxxxxcd9805715xxxxx5387f5e5xxxxx9e6be4_ndiState_xxxxx356924xxxxx13

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 21:25:06