3

For the past week, I have been trying to access Gmail api but it seems impossible. It's do-able in web app but for installable app, no luck. I have used about all plug ins there is and read through all the documentation. I also understand how to do it with http request but then I gotta manually use refresh token which is definitely not the right thing to do.

With the use of various packages, I can get to the point where I get the Authorization code but when I close the web view window, I get errors. Every road seems blocked for me. There isn't enough answers on stack overflow either.

Here is what I've done so far:

  1. I used google api and googleapi auth package to sign in. It gives a signin URL for user consent which is successful in a browser but then it returns a authentication code in the url in the browser and there is no answer where in the package I can use the code to get access token.

  2. I used Google sign in package but it definitely useless because I'm not trying to get public profiles but access token. It gives me error, says I don't have permission.

  3. I used simple auth and oauth2 client package but just like no. 1, I have nowhere to put Authorization code after user consent. I'm slowly loosing all hopes, it's for Android app btw

2 Answers2

1

Try visa - https://github.com/e-oj/visa

Here's an example with Facebook auth (It also supports google):

import 'package:visa/auth-data.dart';
import 'package:visa/fb.dart';

class AuthPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      /// Simply Provide all the necessary credentials
      body: FaceBookAuth().visa.authenticate(
          clientID: '139732240983759',
          redirectUri: 'https://www.e-oj.com/oauth',
          scope: 'public_profile,email',
          state: 'fbAuth',
          onDone: done
      )
    );
  }
}

and the "done" callback:

done(AuthData authData){
  print(authData);

  /// You can pass the [AuthData] object to a 
  /// post-authentication screen. It contaions 
  /// all the user and OAuth data collected during
  /// the authentication process. In this example,
  /// our post-authentication screen is "complete-profile".
  Navigator.pushReplacementNamed(
      context, '/complete-profile', arguments: authData
  );
}
e-oj
  • 209
  • 2
  • 5
0

Here, I have create a basic mail app that loads first 10 mails from the users account.. This code might not be the best possible implementation of the API, but i tried my best with the available information, hope that this code helps.. Github Link

For Google Console, create an app with user-type external and in testing state, for OAuth ClientId with Application type Android. ( Note that: ClientId for Android and iOS devices do not need a client-secret )

For getting the SHA-1 fingerprint following the steps given in this link.

Pranav
  • 1
  • 3