【问题标题】:OAuth2 Redirect URI not validOAuth2 重定向 URI 无效
【发布时间】:2022-08-08 19:44:46
【问题描述】:

我正在尝试使用AppAuth 通过 OAuth2 对 OpenStreetMap 进行身份验证。通过自定义选项卡,我可以检索授权代码,但重定向 URI 不会打开我的应用程序,但会给出一个找不到地址自定义选项卡中的错误。正如你所看到的,当我尝试解决这个问题时,我使用app.example.com 作为主机名,虽然包名是com.example.app,但即使我在重定向 URI 中使用包名作为主机名(并更改这在清单,gradle,osm等中),它仍然不起作用,但会导致无效的重定向 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);

清单.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>

构建.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 虚拟设备。我很困惑

    标签: java android oauth-2.0 appauth


    【解决方案1】:

    使用 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 手机上没有问题。

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 1970-01-01
      • 2014-10-03
      • 2016-01-20
      • 1970-01-01
      • 2018-08-14
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多