Redirect URL Problem Using oauth2_client Package Flutter

113 Views Asked by At

I want to get the access token and refresh token using Google OAuth2. I was unable to pass redirect url to the android app. So, I have tried using web app.

This is My Code so far. I am unable to Figure out what must be customUriScheme and redirectUri. In Azure, I have accessed the refresh token and access token using redirect uri as com.example.calendar://oauth2redirect. But Google doesn't allow me to add this to their redirect URL in the webclient. But the problem is for google webclient https valid redirect scheme is required.

//Instantiate an OAuth2Client...
  GoogleOAuth2Client client = GoogleOAuth2Client(
    customUriScheme: 'my.test.app',
    redirectUri: 'https://my.test.app/oauth2redirect',
  );

  var token = await client.getTokenWithAuthCodeFlow(
    clientId: '',
    clientSecret: '',

    scopes: [
      'https://www.googleapis.com/auth/calendar',
      'https://www.googleapis.com/auth/calendar.readonly'
    ],
    authCodeParams: {"prompt": "consent", "access_type": "offline"},
  );

In Android Manifest I have used this scheme in Azure to get access and refresh tokens to fetch Outlook calendar events. I have tried the same for Google OAuth, but not worked.

<activity android:name="com.linusu.flutter_web_auth_2.CallbackActivity" android:exported="true">
            <intent-filter android:label="flutter_web_auth_2">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="com.example.calendar" /> 
            </intent-filter>
</activity>

// I have also tried this too...

<activity android:name="com.linusu.flutter_web_auth_2.CallbackActivity">
    <intent-filter android:label="flutter_web_auth_2" android:autoVerify="true">
        <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="my.test.app"
                android:pathPrefix="/oauth2redirect" />
    </intent-filter>
</activity>

All the Authentication and permission grants been done after that. This Site can't be reached page is shown. And the tokens are null.

After Authentication Image from WebView

Thank You for Help.

1

There are 1 best solutions below

0
Paras Palli On

I Have Solved this by Directly using the webview_flutter Package. I have used the client ID and client secret from the Google developer console by registering a Web App. And When the Redirect occurs in the webview I queried the access token and other info. You can refer to Google OAuth 2.0 Doc