Flutter/dart: How to handle custom url scheme callback

2.8k Views Asked by At

I'm trying to add Login functionality with Okta Apis in Flutter. In Okta, after the authenticating with username and password it returns a sessionToken which I have to exchange it with authorization api to get api_token.

Here is how the url looks like

https://dev-test.okta.com/oauth2/default/v1/authorize?response_type=token&scope=openid&state=TEST&nonce=TEST&client_id=asdasasdasd&redirect_uri=com.okta.dev-test:/callback&sessionToken=201111ajXZ86MYTJAqGlo9SJrGN02sTRE364aBkmnBVFLq1lX0c7TL3

I have a custom url scheme configured to my app in IOS -> com.okta.dev-test

So once the call is sent, it redirects to the redirectUri where it posts the api_token

com.okta.dev-test:/callback#access_token=eyJraWQiOiJ0Z0EzRERrQU9UaFZfU3g4bXZtOEdFOURtXzhZSGlMZEwzaDQtRzlOQW8wIiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULkNOYkE0OC1DUHRuNVBzSDNwVHR1dHg1VDlyRVA0U0pydkNFMWkzbnJNVTAiLCJpc3MiOiJodHRwczovL2Rldi01NTEzODAub2t0YS5jb20vb2F1dGgyL2RlZmF1bHQiLCJhdWQiOiJhcGk6Ly9kZWZhdWx0IiwiaWF0IjoxNTcxNDQxNTU3LCJleHAiOjE1NzE0NDUxNTcsImNpZCI6IjBvYTFpcHZlYTVtYURCenBFMzU3IiwidWlkIjoiMDB1MWx4M2E4b1pVVFRFNFUzNTciLCJzY3AiOlsib3BlbmlkIl0sInN1YiI6InByYXZpbmFyckBnbWFpbC5jb20ifQ.S9git7PzJF0RpIxULiSAmQFWkNlhG8KvwUZm8UDR68mNgVoUnfK5rXplO3tXZVRxLyzmcFgCXgU36lWc2-hOWF9NUWvgxf7PAmfdNKjdbveMvZmqyNPuiOcDDVvV-h6C0DYgTAo_5VSMPvTI0QerVmgOQ61yYmi12UH319T7OHirsEN1RhIS1ztHLj0CQuXtmSJoX-C5Ko7Mr3PbRkY6OxmgFbyDuTtxWxqTbn5JHXbNggozs48I_nIwurQOLSq8MGAeaUBToiqCfZNhciqpBpEtmJgg4sck-oZsiyA_ZB8HYpBhTmiJNVUsK4Ln6DsI4m2n3RnRd-DuE-KJnDDm7Q&token_type=Bearer&expires_in=3600&scope=openid&state=TEST

Now, when I open this url in a webview,it was able to recognize the url scheme and post the message there. But when I tried to use the http.get to call the url it would error out saying

Exception has occurred.
ArgumentError (Invalid argument(s): No host specified in URI com.okta.dev-test:/callback)

Is there a way to get the access token from the callback and solve this issue using http get? Or is there any other way to achieve this without using a webview ?

Thanks in advance

1

There are 1 best solutions below

0
On

To handle URL scheme on iOS, first ensure that deep linking is configured for iOS. The reason why the error is thrown is because there's nothing configured in you app that can handle com.okta.dev-test:/callback. To handle this path, you need to configure /callback in your Routes.