简体   繁体   中英

OAuth and custom scheme result in a “ERR_UNKNOWN_URL_SCHEME” in Chrome

I've been stuck on this for hours now as the thing was working before but suddenly stopped to behave as expected. I don't really know how and why as I've been re-checking every single line of code in the process without being able to see what's wrong so I'm asking you guys for help.

Alright. So I've a LoginScreen activity with a button starting a new Intent.ACTION_VIEW on click. This start the OAUTH proccess in the browser with a ApiManager.OAUTH_CALLBACK_URI set to stjapp://oauthresponse .

Here's my AndroidManifest.xml part for this activity :

<activity
    android:name=".LoginScreen"
    android:label="@string/application"
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <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="stjapp" android:host="oauthresponse" />
    </intent-filter>
</activity>

How I start the Intent.ACTION_VIEW in my activity :

private View.OnClickListener loginHandler = new View.OnClickListener() {
        public void onClick(View v) {
             OAuthClientRequest request = null;
             try {
                request = OAuthClientRequest
                    .authorizationLocation(ApiManager.OAUTH_AUTHORIZE)
                    .setClientId(ApiManager.CLIENT_ID).setRedirectURI(ApiManager.OAUTH_CALLBACK_URI)
                    .buildQueryMessage();
            } 
            catch (OAuthSystemException e) { e.printStackTrace(); } 
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getLocationUri() + "&response_type=code"));
            startActivity(intent);
        }
    };

And here's a screenshot of what happens in the browser :

在此输入图像描述

There, I'm supposed to get back to my LoginScreen activity and handle the code query parameters within onNewIntent method but ... yeah, the thing doesn't work as expected anymore.

Any help appreciated.

It looks like this is a Chromium bug . The workaround I'm using is a PHP landing page for the RedirectURI that opens the app via JavaScript (which is not affected by that bug):

<script language="javascript">
    window.location = 'myscheme://myhost/?<?=$_SERVER["QUERY_STRING"]?>';
</script>

我有同样的错误,并通过在URL前面添加http://解决它。

In Chrome version 40 and up, this bug has resurfaced, but only if you are manually entering the URL of the redirect page in the address bar. The issue is on the chromium issue tracker here .

The response with the "workaround", pasted from the issue:

OK, I've found out that the window.location redirect via intent will work, AS LONG as it is clicked from an external source or link on page. If you manually enter the address of the redirect page into the address bar, the redirection / opening of the app will fail with said error.

So, sending a link to the redirect page in an email or SMS will work just fine when the user clicks the link. Manually typing the address into the address bar will fail. window.location = chromeUrl; should work just fine.

only if you are manually entering the URL of the redirect page in the address bar. The issue is on the chromium issue tracker here.

The response with the "workaround", pasted from the issue:

OK, I've found out that the window.location redirect via intent will work, AS LONG as it is clicked from an external source or link on page. If you manually enter the address of the redirect page into the address bar, the redirection / opening of the app will fail with said error.

So, sending a link to the redirect page in an email or SMS will work just fine when the user clicks the link. Manually typing the address into the address bar will fail. window.location = chromeUrl; should work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM