コンテンツへスキップ

AndroidでのGoogleログイン

このガイドでは、Capgo Social LoginでAndroid用のGoogleログインを設定する方法を学びます。 一般的なセットアップガイドをすでに読んでいることを前提としています。

このパートでは、AndroidでGoogleログインを設定する方法を学びます。

  1. Android client IDを作成します。

    1. 検索バーをクリックします

      Google Console search bar
    2. credentialsを検索し、APIs and Services(スクリーンショットの2番)をクリックします

      Search results showing credentials option with APIs and Services highlighted
    3. create credentialsをクリックします

      Create credentials button in Google Console
    4. OAuth client IDを選択します

      OAuth client ID option in credentials creation menu
    5. Androidアプリケーションタイプを選択します

      Application type selection with Android option highlighted
    6. Android Studioを開きます

    7. ナビゲーターの一番下にあるGradle Scriptsを見つけます

      Gradle Scripts section in Android Studio project navigator
    8. モジュールappbuild.gradleを見つけます

      build.gradle (Module: app) file in Gradle Scripts section
    9. android.defaultConfig.applicationIdをコピーします。これがGoogle consoleのpackage nameになります

      Build.gradle file showing applicationId configuration
    10. ターミナルを開きます。アプリのandroidフォルダにいることを確認し、./gradlew signInReportを実行します

    Terminal showing gradlew signInReport command
    1. このコマンドの先頭までスクロールします。次のように表示されるはずです。SHA1をコピーします。
    Terminal output showing SHA1 certificate fingerprint
    1. Google Consoleに戻ります。Package NameとしてapplicationIdを入力し、証明書フィールドにSHA1を入力してcreateをクリックします
    Android client creation form with package name and SHA1 fields filled in
  2. Webクライアントを作成します(これはAndroidに必要です)

    1. Google ConsoleのCreate credentialsページに移動します

    2. アプリケーションタイプをWebに設定します

      Application type selection with Web option highlighted
    3. Createをクリックします

      Web client creation form with Create button at bottom
    4. client IDをコピーします。これをJS/TSコードでwebClientIdとして使用します

      Client ID details showing Web client ID to copy
  3. MainActivityを変更します

    1. Android Studioでアプリを開いてください。cap open androidを実行できます

    2. MainActivity.javaを見つけます

      1. appフォルダを開きます

        App folder in Android Studio project navigator
      2. javaを見つけます

        Java folder in Android Studio project structure
      3. MainActivity.javaを見つけてクリックします

        MainActivity.java file in package structure
    3. MainActivity.javaを変更します。次のコードを追加してください

      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. ファイルを保存します

  4. アプリケーションでGoogleログインを使用します

    1. まず、SocialLoginをインポートします

      import { SocialLogin } from '@capgo/capacitor-social-login';
    2. initializeを呼び出します。これは一度だけ呼び出す必要があります。

      // onMountedはVue固有のものです
      // webClientIdは、Android client IDではなく、Webクライアント作成ステップで取得したclient IDです。
      onMounted(() => {
      SocialLogin.initialize({
      google: {
      webClientId: '673324426943-avl4v9ubdas7a0u7igf7in03pdj1dkmg.apps.googleusercontent.com',
      }
      })
      })
    3. SocialLogin.loginを呼び出します。ボタンを作成し、クリック時に次のコードを実行します。

      const res = await SocialLogin.login({
      provider: 'google',
      options: {}
      })
      // レスポンスを処理します
      console.log(JSON.stringify(res))
  5. テスト用のエミュレータを設定します

    1. Device managerに移動してプラスボタンをクリックします

      Device Manager in Android Studio with plus button highlighted
    2. 仮想デバイスを作成します

      Create Virtual Device button in Virtual Device Configuration
    3. Play Storeアイコンがあるデバイスを選択します

      Hardware selection showing devices with Play Store support

      ご覧のとおり、pixel 8Play Storeサービスをサポートしています

    4. nextをクリックします

      Next button in device creation wizard
    5. OSイメージがGoogle Playタイプであることを確認します。Google Playタイプである必要があります

      System image selection showing Google Play type images
    6. nextをクリックします

      Next button in system image selection screen
    7. デバイスを確認します。エミュレータに好きな名前を付けることができます

      Device configuration verification screen with Finish button
    8. Device Managerに移動してシミュレータを起動します

      Device Manager with virtual device listed and play button
    9. シミュレータが起動したら、設定に移動します

      Android emulator showing settings app
    10. Google Playに移動します

    Settings screen with Google Play option
    1. Updateをクリックして約60秒待ちます
    Google Play update screen with Update button
  6. アプリケーションをテストします

    すべて正しく行った場合、Googleログインフローが正しく動作することを確認できます:

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

問題がある場合は、Githubの問題を確認してください。

Googleログインの問題は常にSHA1証明書に関連しています。

開発SHA1証明書を取得できない場合は、カスタムkeystoreを使用してみてください。こちらは、プロジェクトにkeystoreを追加する方法を説明するコメントです。