跳转到内容

Android 上的 Google 登录

在本指南中,您将学习如何为 Android 设置 Capgo Social Login 的 Google 登录。 我假设您已经阅读了通用设置指南

在这部分中,您将学习如何在 Android 中设置 Google 登录。

  1. 创建 Android client ID。

    1. 点击搜索栏

      Google Console 搜索栏
    2. 搜索 credentials 并点击 APIs and Services 选项(屏幕截图中的数字 2)

      显示 credentials 选项的搜索结果,APIs and Services 高亮显示
    3. 点击 create credentials

      Google Console 中的创建凭据按钮
    4. 选择 OAuth client ID

      凭据创建菜单中的 OAuth client ID 选项
    5. 选择 Android 应用程序类型

      应用程序类型选择,Android 选项高亮显示
    6. Open Android Studio

    7. At the very bottom of the navigator, find the Gradle Scripts

      Gradle Scripts section in Android Studio project navigator
    8. Find build.gradle for the module app

      build.gradle (Module: app) file in Gradle Scripts section
    9. Copy the android.defaultConfig.applicationId. This will be your package name in the Google console

      Build.gradle file showing applicationId configuration
    10. Now, open the terminal. Make sure that you are in the android folder of your app and run ./gradlew signInReport

    Terminal showing gradlew signInReport command
    1. Scroll to the top of this command. You should see the following. Copy the SHA1.
    Terminal output showing SHA1 certificate fingerprint
    1. Now, go back to the Google Console. Enter your applicationId as the Package Name and your SHA1 in the certificate field and click create
    Android client creation form with package name and SHA1 fields filled in
  2. Create a web client (this is required for Android)

    1. Go to the Create credentials page in Google Console

    2. Set application type to Web

      Application type selection with Web option highlighted
    3. Click Create

      Web client creation form with Create button at bottom
    4. Copy the client ID, you’ll use this as the webClientId in your JS/TS code

      Client ID details showing Web client ID to copy
  3. Modify your MainActivity

    1. Please open your app in Android Studio. You can run cap open android

    2. Find MainActivity.java

      1. Open the app folder

        App folder in Android Studio project navigator
      2. Find java

        Java folder in Android Studio project structure
      3. Find your MainActivity.java and click on it

        MainActivity.java file in package structure
    3. Modify MainActivity.java. Please add the following code

      import ee.forgr.capacitor.social.login.GoogleProvider;
      import ee.forgr.capacitor.social.login.SocialLoginPlugin;
      import ee.forgr.capacitor.social.login.ModifiedMainActivityForSocialLoginPlugin;
      import com.getcapacitor.PluginHandle;
      import com.getcapacitor.Plugin;
      import android.content.Intent;
      import android.util.Log;
      import com.getcapacitor.BridgeActivity;
      // ModifiedMainActivityForSocialLoginPlugin is VERY VERY important !!!!!!
      public class MainActivity extends BridgeActivity implements ModifiedMainActivityForSocialLoginPlugin {
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode >= GoogleProvider.REQUEST_AUTHORIZE_GOOGLE_MIN && requestCode < GoogleProvider.REQUEST_AUTHORIZE_GOOGLE_MAX) {
      PluginHandle pluginHandle = getBridge().getPlugin("SocialLogin");
      if (pluginHandle == null) {
      Log.i("Google Activity Result", "SocialLogin login handle is null");
      return;
      }
      Plugin plugin = pluginHandle.getInstance();
      if (!(plugin instanceof SocialLoginPlugin)) {
      Log.i("Google Activity Result", "SocialLogin plugin instance is not SocialLoginPlugin");
      return;
      }
      ((SocialLoginPlugin) plugin).handleGoogleLoginIntent(requestCode, data);
      }
      }
      // This function will never be called, leave it empty
      @Override
      public void IHaveModifiedTheMainActivityForTheUseWithSocialLoginPlugin() {}
      }
    4. Save the file

  4. Use Google Login in your application

    1. First, import SocialLogin

      import { SocialLogin } from '@capgo/capacitor-social-login';
    2. Call initialize. This should be called only once.

      // onMounted is Vue specific
      // webClientId is the client ID you got in the web client creation step not the android client ID.
      onMounted(() => {
      SocialLogin.initialize({
      google: {
      webClientId: '673324426943-avl4v9ubdas7a0u7igf7in03pdj1dkmg.apps.googleusercontent.com',
      }
      })
      })
    3. Call SocialLogin.login. Create a button and run the following code on click.

      const res = await SocialLogin.login({
      provider: 'google',
      options: {}
      })
      // handle the response
      console.log(JSON.stringify(res))
  5. Configure the emulator for testing

    1. Go into Device manager and click the plus button

      Device Manager in Android Studio with plus button highlighted
    2. Create a virtual device

      Create Virtual Device button in Virtual Device Configuration
    3. Select any device with a Play Store icon

      Hardware selection showing devices with Play Store support

      As you can see, the pixel 8 supports the Play Store services

    4. Click next

      Next button in device creation wizard
    5. Make sure that the OS image is of type Google Play. IT MUST be of type Google Play

      System image selection showing Google Play type images
    6. Click next

      Next button in system image selection screen
    7. Confirm your device. You can name your emulator as you prefer

      Device configuration verification screen with Finish button
    8. Go into Device Manager and boot up your simulator

      Device Manager with virtual device listed and play button
    9. After the simulator boots up, go into its settings

      Android emulator showing settings app
    10. Go into Google Play

    Settings screen with Google Play option
    1. Click Update and wait about 60 seconds
    Google Play update screen with Update button
  6. Test your application

    If you did everything correctly, you should see the Google login flow working properly:

    Demo of Google login flow on Android showing sign-in process and successful authentication

If you have any issues, please look at the Github issues.

The issues with Google login are ALWAYS related to the SHA1 certificate.

If you cannot get the development SHA1 certificate, try to use a custom keystore. Here is a comment explaining how to add keystore to your project.