Cross platform plugin for doing Google Authentication for Xamarin iOS and Android apps
Google has recently updated its security restrictions for OAuth flow. They are not going to allow native web-views to initiate OAuth flows, but rather are encouraging people to use the OS browsers to do so. The current implementation of the Xamarin.Auth mechanism still uses web-views for initiating the Auth flow which is deprecated and will not work. This plugin attempts to solve that problem by implementing the recommended mechanism for doing Google Auth on native iOS and Android applications and porting it to Xamarin.
Here are the basic steps that you need to follow to add Google Authentication into your Xamarin forms application
-
Setup your Application on the Google developer's console to use OAuth2. Guide here. Make sure you select the Web Application option.
-
Download this repository and copy over the GoogleAuth, Plugin.GoogleAuth, Plugin.GoogleAuth.Android and Plugin.GoogleAuth.iOS folders into your application and add them into your solution in VS or Xamarin Studio.
-
Add references to the Plugin.GoogleAuth and Plugin.GoogleAuth.Abstractions projects into your PCL project.
-
Add references to the Plugin.GoogleAuth.Android and Plugin.GoogleAuth.Abstractions projects into your Android project.
-
Add references to the Plugin.GoogleAuth.iOS and Plugin.GoogleAuth.Abstractions projects into your iOS project. Also add a reference to the GoogleSignin project which resides inside the Plugin.GoogleAuth.iOS/GoogleSignInWrapper folder.
-
Add the
Xamarin Google Play Services
-Base
,Basement
andAuth
Nuget packages (recommended version : 32.961.0) into your Android project. -
In your iOS project, open the Build Settings pane and add these compiler flags under Additional mtouch arguments:
-gcc_flags "-ObjC -framework CoreText -framework SafariServices -framework SystemConfiguration"
-
Also, make sure to add the
URL types
required for handling Google SignIn in the Info.plist file of your iOS application. -
Inside your platform specific projects initialize the Google Authentication service as shown below.
IGoogleAuthenticationService _service; _service = CrossGoogleAuth.Current; Dictionary<string, object> googleSignInConfig = new Dictionary<string, object> { {"clientId", "<client-id>"}, {"context", this} // Android only }; _service.Init(googleSignInConfig);
-
For iOS, override the
OpenUrl
method call in theAppDelegate.cs
file and call theHandleURL
method of the plugin.public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options) { string sourceApplication = options["UIApplicationOpenURLOptionsSourceApplicationKey"].ToString(); NSObject annotation = options["UIApplicationOpenURLOptionsAnnotationKey"] ?? new NSObject(); return _service.HandleURL(url, sourceApplication, annotation); }
-
For Android, override the
OnActivityResult
method call in theMainActivity.cs
file and call theOnActivityResult
method of the Android plugin.protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); var googleAuthService = _service as GoogleAuthImpl; googleAuthService.OnActivityResult(requestCode, resultCode, data); }
Initialize the Google Auth plugin by providing the required keys in the IGoogleAuthenticationService.Init()
method.
Currently supported keys :
- clientId : Client ID for authentication with Google Auth
- context : Application context (Android Only)
To connect to the Google SignIn service and provide a way for the user to sign in to your application, use the IGoogleAuthenticationService.SignIn()
method call.
If the application has already connected, the plugin will try and perform a sign in silently.
To sign out, please call the IGoogleAuthenticationService.SignOut()
To get the status of the sign in call, please implement the IGoogleAuthenticationCallbacks
interface and its methods.
void OnConnectionSucceeded();
void OnConnectionFailed(string errorMessage);
Set the Authentication callbacks via the IGoogleAuthenticationCallbacks.SetAuthenticationCallbacks()
method.
Your application can fetch the following user information in the OnConnectionSucceeded
method.
- Use the
IGoogleAuthenticationCallbacks.GetIdToken
method to get the ID token for the signed in user. This ID token can be used to authenticate the user on the server side. - Use the
IGoogleAuthenticationCallbacks.GetAccountName
method to get the name of the signed in user. - Use the
IGoogleAuthenticationCallbacks.Disconnect
method to disconnect the user account from this app in Google Auth.
- iOS requires the client-id of an iOS client application created on the Google Developer console.
- Android on the other hand requires the client-id of a Web application.
- Convert this into a NuGet package !