Google Sign-In Unity Plugin

repository·newmigration·Indexed 41 days ago

https://github.com/thaina/google-signin-unity

A migrated version of the Google Sign-In Unity plugin (v0.9.0) supporting Android CredentialManager/AuthorizationClient and iOS Google Sign-In SDK v7+. It provides integration for Android, iOS, and desktop, including a PListProcessor for iOS configuration and workflows for integrating with Firebase Auth.

Tokens
2K
Snippets
4
Records
6
Agent score
2%

What's inside Google Sign-In Unity

  1. Build for Android

    newmigration

    To build for Android:

    1. Set the target platform to Android in Build Settings.
    2. In Player Settings, set the Package Name to match the package_name found in your Google configuration file (the entry with client_type: 1).
    3. Configure your Keystore, Key Alias, and Passwords in the Publishing Settings. This keystore must be the same one used to generate the SHA1 fingerprint in the Google Cloud Console.
    4. Run Assets > Play Services Resolver > Android Resolver > Resolve to add the required .aar files to Assets/Plugins/Android.
  2. Build for iOS

    newmigration

    To build for iOS:

    1. Switch the Unity platform to iOS.
    2. Run the Play Services Resolver to add necessary frameworks via CocoPods.
    3. Generate the Xcode project from Unity.
    4. In the generated Xcode project, add the GoogleService-Info.plist file downloaded from the Google Developer website.
  3. Install the Google Sign-In Unity Plugin via UPM

    newmigration

    To install this migrated version of the plugin, add the following dependency to your Unity project's manifest.json file. This version uses the newmigration branch which supports the newer Android CredentialManager and AuthorizationClient APIs.

    Note: You must also include the Google External Dependency Manager (formerly Play Services Resolver) to handle native dependencies.

    {
      "dependencies": {
        "com.google.external-dependency-manager": "https://github.com/googlesamples/unity-jar-resolver.git?path=upm",
        "com.google.signin": "https://github.com/Thaina/google-signin-unity.git#newmigration"
      }
    }
  4. Configure iOS via Info.plist and PListProcessor

    newmigration

    For iOS, the new Google Sign-In SDK (v7.0+) requires GIDClientID and GIDServerClientID to be set in your Info.plist.

    This plugin includes an editor tool called PListProcessor that automatically searches for .plist files in your project. It extracts the CLIENT_ID and WEB_CLIENT_ID from a plist file where the BUNDLE_ID matches your project's bundle identifier.

    Ensure your downloaded Google Cloud Console plist includes the WEB_CLIENT_ID key if you need ServerAuthCode support.

    <!-- Example of a correctly formatted plist for the PListProcessor -->
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    	<key>CLIENT_ID</key> 
    	<string>{YourCloudProjectID}-yyyyyYYYYyyyyYYYYYYYYYYYYYYyyyyyy.apps.googleusercontent.com</string>
    	<key>REVERSED_CLIENT_ID</key>
    	<string>com.googleusercontent.apps.{YourCloudProjectID}-yyyyyYYYYyyyyYYYYYYYYYYYYYYyyyyyy</string>
    	<key>PLIST_VERSION</key>
    	<string>1</string>
    	<key>BUNDLE_ID</key>
    	<string>com.{YourCompany}.{YourProductName}</string>
      <key>WEB_CLIENT_ID</key>
      <string>{YourCloudProjectID}-zzzZZZZZZZZZZZZZZzzzzzzzzzzZZZzzz.apps.googleusercontent.com</string>
    </dict>
    </plist>
  5. Integrate Google Sign-In with Firebase Auth

    newmigration

    To use Google Sign-In as an authentication provider for Firebase, follow these steps:

    1. Configure GoogleSignIn.Configuration to request an ID token and set the WebClientId (the oauth_client with type == 3 from your google-services.json).
    2. Call GoogleSignIn.DefaultInstance.SignIn() or SignInSilently().
    3. In the completion handler, extract the IdToken from the GoogleSignInUser result.
    4. Use Firebase.Auth.GoogleAuthProvider.GetCredential with that token.
    5. Call auth.SignInWithCredentialAsync(credential) to sign the user into Firebase.
    GoogleSignIn.Configuration = new GoogleSignInConfiguration {
      RequestIdToken = true,
      // Copy this value from the google-services.json file.
      // oauth_client with type == 3
      WebClientId = "1072123000000-iacvb7489h55760s3o2nf1xxxxxxxx.apps.googleusercontent.com"
    };
    
    Task<GoogleSignInUser> signIn = GoogleSignIn.DefaultInstance.SignIn ();
    
    TaskCompletionSource<FirebaseUser> signInCompleted = new TaskCompletionSource<FirebaseUser> ();
    signIn.ContinueWith (task => {
      if (task.IsCanceled) {
        signInCompleted.SetCanceled ();
      } else if (task.IsFaulted) {
        signInCompleted.SetException (task.Exception);
      } else {
    
        Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential (((Task<GoogleSignInUser>)task).Result.IdToken, null);
        auth.SignInWithCredentialAsync (credential).ContinueWith (authTask => {
          if (authTask.IsCanceled) {
            signInCompleted.SetCanceled();
          } else if (authTask.IsFaulted) {
            signInCompleted.SetException(authTask.Exception);
          } else {
            signInCompleted.SetResult(((Task<FirebaseUser>)authTask).Result);
          }
        });
      }
    });
  6. Configure GoogleSignInConfiguration

    newmigration

    Initialize the GoogleSignIn.Configuration object to define what data you want to request from the user.

    Crucial Requirement for Android: You MUST provide a WebClientId (the client ID for the OAuth client with client_type == 3). Do not use your Android Client ID here; the system requires the Web Client ID to facilitate the new credential management system.

    For Windows/macOS or Editor testing, the ClientSecret is optional.

    GoogleSignIn.Configuration = new GoogleSignInConfiguration() {
        RequestEmail = true,
        RequestProfile = true,
        RequestIdToken = true,
        RequestAuthCode = true,
        // must be web client ID, not android client ID
        WebClientId = "XXXXXXXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
    #if UNITY_EDITOR || UNITY_STANDALONE
        ClientSecret = "XXXXXX-xxxXXXxxxXXXxxx-xxxxXXXXX" // optional for windows/macos and test in editor
    #endif
    };