Ionic 4 OAuth重定向连接被拒绝

时间:2019-03-12 00:12:36

标签: cordova redirect oauth-2.0 cordova-plugins ionic4

我正在使用ionic / cordova构建Android和iOS应用:ionic版本为:4.11.0&Cordova版本为:8.1.2

我创建了该应用程序,并在chrome上的浏览器中进行了测试,整个应用程序运行良好,包括授权,获取数据等。

当我创建要测试的android应用时,我的问题就开始了。无论我做什么,我都无法获得授权工作流来重定向到应用程序。

这是我尝试过的事情:

1-回调“ http / localhost”使连接被拒绝,无论我在config.xml,cordova Web视图插件和cordova自定义网址方案插件中尝试了什么。

无论我尝试修改config.xml并使用与上面相同的cordova插件,2-回调“ myapp:// callback”都会给err_unkown_url_schema“。

我正在LGG6 Android 8.0.0版上进行测试。尚未在iOS上进行测试。

代码段:

var uniqueWindowId = 'authorization-' + this.client_id
var params = 'titlebar=no, location=yes'
    var loginWindow = window.open(this.authCodeURL, uniqueWindowId, params);

    loginWindow.addEventListener(
      'message',
      e => {
        var oauthMessage = e.data;
        alert(oauthMessage);
        loginWindow.close();
        loginWindow.opener.postMessage(oauthMessage, 'https://localhost/');
      },
      false
    );

这是设备通过Android Studio发送的日志

Connecting to io.ionic.starter
Connected to the target VM, address: 'localhost:8600', transport: 'socket'
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote64: Late-enabling -Xcheck:jni
W/ActivityThread: Application io.ionic.starter is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/zygote64: Debugger is active
I/System.out: Debugger has connected
    waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1419)
I/InstantRun: starting instant run server: is main process
I/CordovaLog: Changing log level to DEBUG(3)
I/CordovaActivity: Apache Cordova native platform version 7.1.4 is starting
D/CordovaActivity: CordovaActivity.onCreate()
I/WebViewFactory: Loading com.android.chrome version 72.0.3626.121 (code 362612152)
I/cr_LibraryLoader: Time to load native libraries: 7 ms (timestamps 8989-8996)
I/chromium: [INFO:library_loader_hooks.cc(42)] Chromium logging enabled: level = 0, default verbosity = 0
I/cr_LibraryLoader: Expected native library version number "72.0.3626.121", actual native library version number "72.0.3626.121"
W/cr_ChildProcLH: Create a new ChildConnectionAllocator with package name = com.android.chrome, sandboxed = true
I/cr_BrowserStartup: Initializing chromium process, singleProcess=false
W/ResourceType: Failure getting entry for 0x7f120538 (t=17 e=1336) (error -2147483647)
D/IonicWebViewEngine: Ionic Web View Engine Starting Right Up 1...
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
D/SystemWebViewEngine: CordovaWebView is running on device made by: LGE
D/PluginManager: init()
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
D/OpenGLRenderer: HWUI GL Pipeline
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
I/zygote64: Do partial code cache collection, code=28KB, data=25KB
I/zygote64: After code cache collection, code=27KB, data=25KB
    Increasing code cache capacity to 128KB
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
D/CordovaWebViewImpl: >>> loadUrl(file:///android_asset/www/index.html)
I/AudioManagerEx: AudioManagerEx created
W/cr_media: Requires BLUETOOTH permission
D/CordovaActivity: Started the activity.
I/Adreno: QUALCOMM build                   : 14c0496, Ida6448821d
    Build Date                       : 01/08/18
    OpenGL ES Shader Compiler Version: EV031.22.00.01
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.6.5.R1.08.00.00.312.043
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
D/CordovaActivity: Resumed the activity.
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
W/AudioCapabilities: Unsupported mime audio/x-lg-alac
W/AudioCapabilities: Unsupported mime audio/x-lg-flac
W/AudioCapabilities: Unsupported mime audio/adpcm
W/AudioCapabilities: Unsupported mime audio/g726
W/AudioCapabilities: Unsupported mime audio/lg-wma-voice
W/AudioCapabilities: Unsupported mime audio/x-lg-ms-wma
W/VideoCapabilities: Unsupported mime video/mjpg
W/VideoCapabilities: Unsupported mime video/theora
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
    Unrecognized profile 2130706434 for video/avc
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
    Unrecognized profile 2130706434 for video/avc
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
W/VideoCapabilities: Unrecognized profile 2130706434 for video/avc
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@fdd775d time:801221046
W/VideoCapabilities: Unsupported mime video/dolby-vision
W/VideoCapabilities: Unsupported mime video/dolby-vision
W/VideoCapabilities: Unsupported mime video/mp4v-esdp
I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
W/cr_CrashFileManager: /data/user/0/io.ionic.starter/cache/WebView/Crash Reports does not exist or is not a directory
D/CordovaWebViewImpl: onPageDidNavigate(file:///android_asset/www/index.html)
D/CordovaWebViewImpl: onPageFinished(file:///android_asset/www/index.html)
D/SERVER: Handling local request: http://localhost/
D/CordovaWebViewImpl: onPageDidNavigate(http://localhost/)
D/SERVER: Handling local request: http://localhost/cordova.js
D/SERVER: Handling local request: http://localhost/build/main.css
D/SERVER: Handling local request: http://localhost/build/polyfills.js
D/SERVER: Handling local request: http://localhost/build/vendor.js
D/SERVER: Handling local request: http://localhost/build/main.js
D/JsMessageQueue: Set native->JS mode to EvalBridgeMode
D/SERVER: Handling local request: http://localhost/cordova_plugins.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-device/www/device.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-inappbrowser/www/inappbrowser.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-ionic-keyboard/www/android/keyboard.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-ionic-webview/src/www/util.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-splashscreen/www/splashscreen.js
I/zygote64: Do partial code cache collection, code=61KB, data=56KB
    After code cache collection, code=61KB, data=56KB
    Increasing code cache capacity to 256KB
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-statusbar/www/statusbar.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-sqlite-storage/www/SQLitePlugin.js
D/SystemWebChromeClient: http://localhost/build/vendor.js: Line 4325 : Angular is running in the development mode. Call enableProdMode() to enable the production mode.
I/chromium: [INFO:CONSOLE(4325)] "Angular is running in the development mode. Call enableProdMode() to enable the production mode.", source: http://localhost/build/vendor.js (4325)
D/SystemWebChromeClient: http://localhost/build/main.js: Line 998 : ################################################################################ home.ts constructor
I/chromium: [INFO:CONSOLE(998)] "################################################################################ home.ts constructor", source: http://localhost/build/main.js (998)
D/SERVER: Handling local request: http://localhost/assets/icon/icon.png
D/SERVER: Handling local request: http://localhost/assets/fonts/ionicons.woff2?v=3.0.0-alpha.3
D/SERVER: Handling local request: http://localhost/assets/fonts/roboto-medium.woff2
D/SERVER: Handling local request: http://localhost/assets/fonts/roboto-regular.woff2
D/SystemWebChromeClient: http://localhost/build/vendor.js: Line 140284 : Ionic Native: deviceready event fired after 1716 ms
I/chromium: [INFO:CONSOLE(140284)] "Ionic Native: deviceready event fired after 1716 ms", source: http://localhost/build/vendor.js (140284)
I/App: WARNING: Back Button Default Behavior will be overridden.  The backbutton event will be fired!
D/CordovaWebViewImpl: onPageFinished(http://localhost/)
D/SERVER: Handling local request: http://localhost/assets/icon/favicon.ico
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/zygote64: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()
I/zygote64: Do full code cache collection, code=123KB, data=107KB
I/zygote64: After code cache collection, code=94KB, data=64KB
D/InAppBrowser: target = _self
D/InAppBrowser: in self
D/InAppBrowser: isUrlWhiteListed [class java.lang.String]
D/InAppBrowser: loading in InAppBrowser
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
I/zygote64: Do partial code cache collection, code=118KB, data=94KB
    After code cache collection, code=118KB, data=94KB
    Increasing code cache capacity to 512KB
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
I/AssistStructure: Flattened final assist data: 520 bytes, containing 1 windows, 2 views
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP

我在屏幕上收到的消息是:

the webpage at http://localhost/?code=......@state=..... could nopt be loaded becuase:

net::ERR_CONNECTION_REFUSED

1 个答案:

答案 0 :(得分:0)

如果您要通过应用外部的网址进行oauth验证,我建议使用inappbrowser ionic native插件。

https://ionicframework.com/docs/native/in-app-browser/

您尝试过吗?通常,使用inapp浏览器作为oauth的这种方法已成为一种反模式,随着Ionic Capacitor的发展,他们已经使用iOS的SafariViewController和Android的Chrome自定义标签来实现解决方案,而不是使用inappbrowser,但是仍然非常我认为测试版中有很多内容。

https://capacitor.ionicframework.com/docs/apis/browser/

这是有关使用inappbrowser的OAuth的过时文章,但在涵盖插件的基本用法方面做得很好:

https://www.thepolyglotdeveloper.com/2016/01/using-an-oauth-2-0-service-within-an-ionic-2-mobile-app/

有关为什么它成为反模式的文章: https://medium.com/@jlchereau/stop-using-inappbrowser-for-your-cordova-phonegap-oauth-flow-a806b61a2dc5

关于您的特定问题,我还将检查控制台日志,并确保它不是CORS问题,还要检查您的白名单中config.xml中的导航。