> For the complete documentation index, see [llms.txt](https://easonwang.gitbook.io/web_advance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easonwang.gitbook.io/web_advance/react-native/googla-oauth/android.md).

# Android

### 教學

<https://github.com/react-native-google-signin/google-signin/blob/master/docs/android-guide.md>

1.新增 Firebase 專案，然後產生金鑰後填入

<https://stackoverflow.com/questions/15727912/sha-1-fingerprint-of-keystore-certificate>

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-MLfspG-qoWeJ-48Z-qV%2F-MLg-NrSZfvgW-GZpnqA%2F%E6%88%AA%E5%9C%96%202020-11-09%20%E4%B8%8B%E5%8D%883.55.00.png?alt=media\&token=b09017c6-ed2e-454b-ad8c-4b6f410a91cb)

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-MLfspG-qoWeJ-48Z-qV%2F-MLg-KBaxJ4yXOBTBoa9%2F%E6%88%AA%E5%9C%96%202020-11-09%20%E4%B8%8B%E5%8D%883.54.45.png?alt=media\&token=74bbda5c-c65f-444a-91cc-a3932309c4bf)

之後按下一步加上必要的 build.gradle 參數。

2.app/build.gradle

```
dependencies {
    implementation platform('com.google.firebase:firebase-bom:26.0.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation(project(":react-native-google-signin"))
    ....
```

3.外層的 project/build.gradle

```
    ext {
        ...
        googlePlayServicesAuthVersion = "16.0.1"
    }
    repositories {
        ...
        google()
    }
    dependencies {
        ....
        classpath("com.android.tools.build:gradle:3.5.3")
        classpath 'com.google.gms:google-services:4.3.4'
```

4\. setting.gradle 加上

```
include ':react-native-google-signin', ':app'
project(':react-native-google-signin').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/google-signin/android')
```

5.MainApplication.java

> React Native 0.6 以後因為 Auto linking 不用此步驟

```java
        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // 加上下面這行
          packages.add(new RNGoogleSigninPackage());
          return packages;
        }
```

### 範例

```javascript
import {GoogleSignin, statusCodes} from '@react-native-community/google-signin';

GoogleSignin.configure();

const handleGoogleSignIn = async () => {
    try {
      await GoogleSignin.hasPlayServices();
      const userInfo = await GoogleSignin.signIn();
      console.log(userInfo)
    } catch (error) {
      console.log(error)
      if (error.code === statusCodes.SIGN_IN_CANCELLED) {
        // user cancelled the login flow
      } else if (error.code === statusCodes.IN_PROGRESS) {
        // operation (e.g. sign in) is in progress already
      } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
        // play services not available or outdated
      } else {
        // some other error happened
      }
    }
  };
```
