我正在使用这个 OAuth 库 https://github.com/openid/AppAuth-Android 我已经注册了一个自定义方案
<activity android:name="net.openid.appauth.RedirectUriReceiverActivity">
<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="kronos"
android:host="oauth2"/> <!-- Redirect URI scheme -->
</intent-filter>
</activity>
我有这些方法:
private void authorize() {
AuthorizationRequest authRequest = new AuthorizationRequest.Builder(
mServiceConfiguration,
"...", // Client ID
ResponseTypeValues.CODE,
Uri.parse("kronos://oauth2/callback") // Redirect URI
).setScope("auth").build();
AuthorizationService service = new AuthorizationService(this);
Intent intent = service.getAuthorizationRequestIntent(authRequest);
startActivityForResult(intent, REQUEST_CODE_AUTH);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE_AUTH) {
Log.d(TAG, "get the token");
return;
}else{
Log.d(TAG, "wrong code");
return;
}
它工作正常,但现在我想处理一个自定义 URL,例如“kronos://something”,所以到目前为止有 2 个问题:
-可以在onActivityResult中进行处理吗?如何处理?
-我可以在 Chrome 中调用这个自定义协议(protocol)来测试(就像在 iOS 中使用 safari 一样)吗?
编辑:更准确地说,当用户注销以删除 OAuth token 时,我收到的是自定义 URL
编辑2 我已将其添加到 list 中
<activity android:name=".activity_logoff">
<intent-filter>
<data android:scheme="kronos"
android:host="something"/> <!-- Redirect URI scheme -->
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
但我不知道如何让应用程序拦截所有 url“kronos://something”
编辑3 我现在已经创建了可能需要的 Intent
Uri logoutUri = Uri.parse("kronos://something");
Intent logoutIntent1 = new Intent(Intent.ACTION_VIEW,logoutUri);
编辑4 我已经添加了
logoutIntent1.setPackage(getApplicationContext().getPackageName());
但是下一步该做什么
最佳答案
你想要这个kronos://something
做什么?它与身份验证响应有什么关系吗?如果不是,那么 oAuth 在这里没有任何作用。
您即将声明 Activity_logoff。您所需要做的就是调用:
context.startActivity(logoutIntent1)
- 其中上下文可以是任何 Activity。
关于java - 如何使用java在android中打开自定义方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61817846/