oauth-2.0 OAuth2重定向URI无效

cygmwpex  于 2022-10-31  发布在  其他
关注(0)|答案(1)|浏览(417)

我正在尝试使用AppAuth通过OAuth2向OpenStreetMap进行身份验证。通过自定义选项卡,我可以检索授权码,但重定向URI无法打开我的应用程序,而是在自定义选项卡中显示 Address Not Found 错误。正如您所看到的,我在尝试解决此问题时使用了app.example.com作为主机名,尽管软件包名称为com.example.app。但即使我在重定向URI中使用包名作为主机名(并在manifest、gradle、osm等中进行更改),它仍然不起作用,并导致 Invalid Redirect URI 错误。因此,我会假设重定向URI有什么地方不太正确,但我无法找出它是什么。
此外,我不能使用自定义方案,如OSM only accepts https redirect URIs
MainActivity.java:

private static final String CLIENT_ID = ...;
private static final String REDIRECT_URI = "https://app.example.com/oauth2redirect";
...
AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(
            authorizationServiceConfiguration,
            CLIENT_ID,
            ResponseTypeValues.CODE,
            Uri.parse(REDIRECT_URI));
builder.setScopes("write_api", "read_prefs");

AuthorizationRequest request = builder.build();
Intent authorizationIntent = authorizationService.getAuthorizationRequestIntent(request);
startActivityForResult(authorizationIntent, REQUEST_CODE);

Manifest.xml:

<activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>
            <action android:name="com.example.app.HANDLE_AUTHORIZATION_RESPONSE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name="net.openid.appauth.RedirectUriReceiverActivity"
        android:exported="true">
        <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="https"
                android:host="app.example.com"
                android:pathPrefix="/oauth2redirect"/>
        </intent-filter>
 </activity>

build.gradle

android {
...

defaultConfig {
    applicationId "com.example.app"
    ...
    manifestPlaceholders = [
            appAuthRedirectScheme: 'app.example.com'
    ]
}

在OSM中,我将重定向URI设置为https://app.example.com/oauth2redirect
奇怪的是,昨天它还能用,但从今天起就不能用了。我恢复了所有的更改,重置了应用程序,删除了所有的数据,重新启动了我的手机,但还是不能让它再用了。
我已经尝试显示尽可能少的代码所需,请让我知道,如果你需要进一步的信息来解决这个问题。
编辑:我刚刚注意到它在Pixel 5 API 30虚拟设备上工作,但在我的真实的设备(小米Poco X3 Pro API 30)和Nexus 6 API 30虚拟设备上都不工作,我相当困惑

axkjgtzd

axkjgtzd1#

使用HTTP重定向URI需要在build.gradle文件中进行这些设置,并且还需要通过托管的assetlinks.json file进行应用链接注册:

manifestPlaceholders = [
    'appAuthRedirectScheme': 'https'
]

存在一个已知问题,即OAuth HTTPS响应不会自动从Chrome自定义选项卡返回到应用程序,除非首先有用户手势,例如单击按钮。
如果可能,作为此问题的初始解决方案,您可能希望显示一个OAuth屏幕,以允许用户选择他们要使用的帐户,方法是使用prompt=select_account

AuthorizationRequest.Builder authRequestBuilder = new AuthorizationRequest.Builder(
                Objects.requireNonNull(authState.getAuthorizationServiceConfiguration()),
                clientId,
                ResponseTypeValues.CODE,
                Uri.parse(redirectUri))
                .setScope(authScope)
                .setPrompt("select_account");

如果你想与我的演示应用进行比较,请尝试运行我的演示应用,看看它是否能给你带来一些灵感。注意,最近我在使用API 31及更高版本的模拟器上发现了一些问题,但在我运行Android 12的Google Pixel手机上却没有问题。

相关问题