我正在尝试使用 Android Chrome 自定义选项卡实现 OAuth2 流,但是当 Chrome 自定义选项卡收到带有我的应用程序位置/方案的 302 时,我的应用程序总是关闭(没有崩溃)。
如果我创建一个带有 ahref 链接的 HTML 页面并手动触摸它,Chrome 自定义选项卡会正确切换到我的应用程序。
似乎在 Chrome 自定义选项卡中处理服务器 302 重定向时,它不会正确处理我的自定义应用程序方案......但为什么呢?
如果我在股票浏览器或 WebView 中尝试相同的重定向 URL,那么一切正常。
这是我当前的设置:
MainActiviy.java
Button btnChromeCustomTab = (Button) findViewById(R.id.btnChromeCustomTab);
btnChromeCustomTab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
String packageName = CustomTabsHelper.getPackageNameToUse(MainActivity.this);
customTabsIntent.intent.setPackage(packageName);
Uri theLocationUri = Uri.parse(URL);
customTabsIntent.launchUrl(MainActivity.this, theLocationUri);
}
});
AndroidManifest.xml
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:label="@string/filter_title">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myappscheme" android:host="oauth" />
</intent-filter>
</activity>
这是应用收到的带有 HTTP 302 代码的重定向 URL:
myappscheme://oauth?code=1234567&state=tokenCheck123
构建.gradle
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "de.myapptest.webviewtest"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:customtabs:23.0.0+'
}
谢谢你的帮助...