使用openid AppAuth登录,成功登录后不会调用registerForActivityResult,而是在登录页面上停止。预期:成功登录后,网页应关闭并重定向到调用"活动"。以下是示例代码:
private lateinit var authorizationService: AuthorizationService
lateinit var config: AuthorizationServiceConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.myLayout)
authorizationService = AuthorizationService(this)
binding.btnLogin.setOnClickListener { getAuth() }
}
private fun getAuth() {
config = AuthorizationServiceConfiguration(
Uri.parse("{{Domain in URL}}/oauth2/v2.0/authorize"),
Uri.parse("{{Domain in URL}}/oauth2/v2.0/token?p=B2C_1_SUSI_Test")
)
var params: HashMap<String, String> = HashMap()
params["p"] = "B2C_1_SUSI_Test"
val request = AuthorizationRequest
.Builder(config, CLIENT_ID, ResponseTypeValues.CODE, Uri.parse(URL_AUTH_REDIRECT))
.setScopes(
"openid", "profile", "email",
"{{Domain in URL}}/api/app.user.basic"
)
.setRedirectUri(Uri.parse(URL_AUTH_REDIRECT))
.setAdditionalParameters(params)
.build()
val intent = authorizationService.getAuthorizationRequestIntent(request)
launcher.launch(intent)
}
private val launcher =
registerForActivityResult(StartActivityForResult()) { result ->
Log.d("TAG", "registerForActivityResult = $result")
if (result.resultCode == Activity.RESULT_OK) {
val ex = AuthorizationException.fromIntent(result.data!!)
val result = AuthorizationResponse.fromIntent(result.data!!)
if (ex != null) {
Log.e("TAG", "registerForActivityResult : Error $ex")
} else {
// val secret = ClientSecretBasic(GITHUB_CLIENT_SECRET)
val token = result?.createTokenExchangeRequest()
Log.d("TAG", "Token : $token")
}
}
}
Android清单文件如下
<activity
android:name="net.openid.appauth.RedirectUriReceiverActivity"
android:exported="true"
tools:node="replace">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="{{app package}}" />
<data
android:host="{{domain}}"
android:path="/oauth2redirect"
android:scheme="https" />
</intent-filter>
</activity>
请帮我解决这个问题。
我遇到了同样的问题。以下是适用于我的AppAuthv0.11.1
。
-
在
AndroidManifest.xml
文件中:删除<application android:noHistory="true" />
选项(如果存在(,或者将其设置为false
。 -
在同一文件中,将以下意向过滤器添加到具有登录按钮的"活动"中:
<intent-filter>
<action android:name="com.example.yourproject.HANDLE_AUTHORIZATION_RESPONSE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
就是这样。这么多的配置足以让它工作。
以下是我在GitHub用户身份验证内容中的身份验证代码:
private val launcher = registerForActivityResult(StartActivityForResult()){
if (it.resultCode == RESULT_OK) {
val ex = AuthorizationException.fromIntent(it.data!!)
val result = AuthorizationResponse.fromIntent(it.data!!)
if (ex != null){
Log.e("Github Auth", "launcher: $ex")
} else {
val secret = ClientSecretBasic(GITHUB_CLIENT_SECRET)
val tokenRequest = result?.createTokenExchangeRequest()
service.performTokenRequest(tokenRequest!!, secret) {res, exception ->
if (exception != null){
Log.e("Github Auth", "launcher: ${exception.error}" )
} else {
val token = res?.accessToken
viewModel.setToken(token!!)
// Move to Github screen
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
}
}
}
private fun githubAuth() {
val redirectUri = Uri.parse("com.example.yourproject://oauth2redirect")
val authorizeUri = Uri.parse("https://github.com/login/oauth/authorize")
val tokenUri = Uri.parse("https://github.com/login/oauth/access_token")
val config = AuthorizationServiceConfiguration(authorizeUri, tokenUri)
val request = AuthorizationRequest
.Builder(config, GITHUB_CLIENT_ID, ResponseTypeValues.CODE, redirectUri)
.setScopes("user repo admin")
.build()
val intent = service.getAuthorizationRequestIntent(request)
launcher.launch(intent)
}
这篇文章很有帮助:https://medium.com/androiddevelopers/authenticating-on-android-with-the-appauth-library-7bea226555d5