# @codetrix-studio/capacitor-google-auth 中文文档

规范地址：https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/

版本：3\.4\.0-rc\.4
npm 元数据更新时间：2024-05-01T06:53:46.677Z
中文译文更新时间：2026-11-10T08:10:10.110Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40codetrix-studio/capacitor-google-auth
原文对照：https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/?lang=original
项目主页：https://github.com/CodetrixStudio/CapacitorGoogleAuth#readme
源码仓库：https://github.com/CodetrixStudio/CapacitorGoogleAuth

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：159 kB，39 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：@capacitor/core ^6\.0\.0
- 首次发布：2019-05-19T17:59:28.914Z
- 最新版本发布：2024-05-01T06:53:46.490Z
- 近 30 天下载量：223599（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：44（近 12 个月 0 个）

## 中文简介

电容器的Google Auth插件.

## 安装

```sh
npm install @codetrix-studio/capacitor-google-auth
```

也可以使用：`pnpm add @codetrix-studio/capacitor-google-auth`、`yarn add @codetrix-studio/capacitor-google-auth`、`bun add @codetrix-studio/capacitor-google-auth`

## README

# Google Auth 电容控制器

**`@codetrix-studio/capacitor-google-auth`**

**第6条**

Google Auth 的电容插件.

  

[![npm](https://img.shields.io/npm/v/@codetrix-studio/capacitor-google-auth)](https://www.npmjs.com/package/@codetrix-studio/capacitor-google-auth) [![npm](https://img.shields.io/npm/dt/@codetrix-studio/capacitor-google-auth)](https://www.npmjs.com/package/@codetrix-studio/capacitor-google-auth) [![npm](https://img.shields.io/npm/dw/@codetrix-studio/capacitor-google-auth)](https://www.npmjs.com/package/@codetrix-studio/capacitor-google-auth) [![Dependents (via libraries.io)](https://img.shields.io/librariesio/dependents/npm/@codetrix-studio/capacitor-google-auth)](https://libraries.io/npm/@codetrix-studio%2Fcapacitor-google-auth) [![install size](https://packagephobia.com/badge?p=@codetrix-studio/capacitor-google-auth)](https://packagephobia.com/result?p=@codetrix-studio/capacitor-google-auth)

## 捐款

PR受到欢迎并非常赞赏,它使这个插件与Capacitor和官方Google Auth平台库的功能等同.

尽量遵循良好的代码惯例. 你甚至可以帮助保持 包含的演示更新.

不与Google Auth 官方库对齐的特性的公关不便.

(我们在这里是初学者友好的)

## 安装

#### 1\. 安装包

```sh
npm i --save @codetrix-studio/capacitor-google-auth

# pnpm 
pnpm add @codetrix-studio/capacitor-google-auth

# yarn 
yarn add @codetrix-studio/capacitor-google-auth
```

#### 2\. 更新电容器道具

```sh
npx cap update
```

## 更新

如果需要迁移到不同的控制器版本 [ưμ㼯a](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-migration-guide)。 。 。 .

## 使用量

### 韦伯

注册插件并手动初始化

```ts
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';

// use hook after platform dom ready
GoogleAuth.initialize({
  clientId: 'CLIENT_ID.apps.googleusercontent.com',
  scopes: ['profile', 'email'],
  grantOfflineAccess: true,
});
```

或者如果需要使用元标记(可选) :

```html
<meta name="google-signin-client_id" content="{your client id here}" />
<meta name="google-signin-scope" content="profile email" />
```

#### 选项

-   `clientId` - 该应用程序的客户端ID,在Google开发者控制台中找到并创建.
-   `scopes` - 与 [配置](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-Configure) 范围
-   `grantOfflineAccess` —布尔,默认值 `false`,设定当用户不在浏览器时,您的应用程序需要刷新访问符.

用这个

```ts
GoogleAuth.signIn();
```

#### 角质

输入钩子

```ts
// app.component.ts
constructor() {
  this.initializeApp();
}

initializeApp() {
  this.platform.ready().then(() => {
    GoogleAuth.initialize()
  })
}
```

在函数中签名

```ts
async googleSignIn() {
  let googleUser = await GoogleAuth.signIn();

  /*
    If you use Firebase you can forward and use the logged in Google user like this:
  */
  const credential = auth.GoogleAuthProvider.credential(googleUser.authentication.idToken);
  return this.afAuth.auth.signInAndRetrieveDataWithCredential(credential);
}
```

#### 第3组

```vue
<script setup lang="ts">
import { defineComponent, onMounted } from 'vue';
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';

onMounted(() => {
  GoogleAuth.initialize();
});

async function logIn() {
  const response = await GoogleAuth.signIn();
  console.log(response);
}
</script>
```

或看见更多 [GoogleAuth-Vue3-实例](https://github.com/reslear/CapacitorGoogleAuth-Vue3-example)

### iOS系统

1.  在 Google 云端控制台创建证书 **IOS 客户端标识** 获得 **客户身份** 财务报告和审定财务报表 **iOS URL 计划**
    
2.  添加 **标识符** `REVERSED_CLIENT_ID` 作为 **URL 计划** 改为 `Info.plist` 从 **iOS URL 计划**  
    (X代码:App - 目标/App - 信息 - URL) 类型,单击加图标)
    
3.  设定 **客户身份** 其中一个方式:
    
    1.  设定为 `capacitor.config.json`
        -   `iosClientId` iOS 的特定密钥
        -   `clientId` - 或Android和iOS的常见密钥
    2.  下载 `GoogleService-Info.plist` 文件为 `CLIENT_ID` 复制到 **欧斯/阿普/阿普** 必须通过Xcode进行索引.

插件首先使用 `iosClientId` 如果没有找到用途 `clientId` 如果没有找到使用值 `CLIENT_ID` 从文件 `GoogleService-Info.plist`

### 机器人

设定 **客户身份** 数字 :

1.  单位 `capacitor.config.json`
    
    -   `androidClientId` - Android的特定密钥
    -   `clientId` - 或Android和iOS的常见密钥
2.  或设置在您的内部 `strings.xml`
    

插件首先使用 `androidClientId` 如果没有找到用途 `clientId` 如果没有找到使用值 `server_client_id` 从文件 `strings.xml`

```xml
<resources>
  <string name="server_client_id">Your Web Client Key</string>
</resources>
```

**刷新方法**

当应用程序初始化以确认用户是否正在登录时,应该使用这种方法 。 如果为真, 方法将返回一个访问Token, idToken和一个空的刷新Token .

```ts
checkLoggedIn() {
    GoogleAuth.refresh()
        .then((data) => {
            if (data.accessToken) {
                this.currentTokens = data;
            }
        })
        .catch((error) => {
            if (error.type === 'userLoggedOut') {
                this.signin()
            }
        });
}
```

## 配置

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| 客户端ID | 字符串 | 该应用程序的客户端ID,在Google开发者控制台中找到并创建. |
| iosclientId 坐标 | 字符串 | IOS 的特定客户端 ID 密钥 |
| 机器人客户端 | 字符串 | Android 的特定客户端ID密钥 |
| 范围 | 字符串 \[\] | 您可能需要请求访问 Google API 的范围  
https://developers.google.com/identity/protocols/oauth2/scopes 互联网档案馆的存檔,存档日期2014-12-2 |
| 服务器客户端Id | 字符串 | 此客户端ID 用于离线访问和服务器侧处理 |
| 强制代码FreshToken | 布尔 | 强制用户选择电子邮件地址以重生 AuthCode  
用于获得有效的刷新工具(在iOS和Android上的工作) |

在根中提供配置 `capacitor.config.json`

```json
{
  "plugins": {
    "GoogleAuth": {
      "scopes": ["profile", "email"],
      "serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
      "forceCodeForRefreshToken": true
    }
  }
}
```

或进入 `capacitor.config.ts`

```ts
/// <reference types="'@codetrix-studio/capacitor-google-auth'" />

const config: CapacitorConfig = {
  plugins: {
    GoogleAuth: {
      scopes: ['profile', 'email'],
      serverClientId: 'xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
      forceCodeForRefreshToken: true,
    },
  },
};

export default config;
```

## API 密码

-   [`initialize(...)`](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-initialize)
-   [`signIn()`](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-signin)
-   [`refresh()`](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-refresh)
-   [`signOut()`](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-signout)
-   [接口](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-interfaces)

### 初始化(...)

```typescript
initialize(options?: InitOptions) => void
```

初始化了GoogleAuthPlugin,加载了chaffi库并设置了插件.

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| **`options`** | `[InitOptions](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-initoptions)` | \- 可选初始化选项. |

**自:** 3.1.0 (中文(简体) )

* * *

### 签名IN ()

```typescript
signIn() => Promise<User>
```

启动登录进程,并返回以用户信息解决的保证.

**回返情况:** `Promise<[User](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-user)>`

* * *

### 刷新( )

```typescript
refresh() => Promise<Authentication>
```

刷新认证符并返回以更新认证细节解决的保证 .

**回返情况:** `Promise<[Authentication](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-authentication)>`

* * *

### 符号输出( C)

```typescript
signOut() => Promise<any>
```

签署用户并返回承诺.

**回返情况:** `Promise<any>`

* * *

### 接口

#### 输入选项

| Prop | 类型 | 说明 | 默认 | 自兹 |
| --- | --- | --- | --- | --- |
| **`clientId`** | `string` | 该应用程序的客户端ID,在Google开发者控制台中找到并创建. 常见于Android或iOS. 默认在配置中被定义. |  | 3.1.0 (中文(简体) ) |
| **`scopes`** | `string[]` | 指定访问 Google API 所需的范围 默认在配置中被定义. |  |  |
| **`grantOfflineAccess`** | `boolean` | 设定当用户不在浏览器时您的应用程序需要刷新访问符 。 反应使用 `serverAuthCode` 键 | `false` | 3.1.0 (中文(简体) ) |

#### 用户

| Prop | 类型 | 说明 |
| --- | --- | --- |
| **`id`** | `string` | 用户独有的标识符 . |
| **`email`** | `string` | 与用户相关的电子邮件地址 . |
| **`name`** | `string` | 用户全名. |
| **`familyName`** | `string` | 用户的姓氏(上名). |
| **`givenName`** | `string` | 用户的给定名称(名). |
| **`imageUrl`** | `string` | 用户配置图片的 URL. |
| **`serverAuthCode`** | `string` | 服务器认证代码. |
| **`authentication`** | `[Authentication](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/#readme-authentication)` | 认证细节,包括访问、刷新和身份标志. |

#### 认证

| Prop | 类型 | 说明 |
| --- | --- | --- |
| **`accessToken`** | `string` | 在认证过程中获得的访问令牌 . |
| **`idToken`** | `string` | 在认证过程中获得的ID 令牌 . |
| **`refreshToken`** | `string` | 刷新标志. |

## 移徙指南

#### 从3.3.x移到3.4.x

安装3.4.x版本 :

```sh
npm i --save @codetrix-studio/capacitor-google-auth@^3.4
```

执行您的工程指令 [更新到电容 6](https://capacitorjs.com/docs/next/updating/6-0)。 。 。 .

#### 从3.2.x移至3.3.x

安装3.3.x版本 :

```sh
npm i --save @codetrix-studio/capacitor-google-auth^3.3
```

执行您的工程指令 [从电容4更新到电容5](https://capacitorjs.com/docs/updating/5-0)。 。 。 .

#### 从3.2.1移至3.2.2

页:1 `Android` 在文件中 `MainActivity.onCreate`

```diff
- this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
-   add(GoogleAuth.class);
- }});
+ this.registerPlugin(GoogleAuth.class);
```

#### 从 3.1.x 移到 3.2.x

安装3.2.x版本 :

```sh
npm i --save @codetrix-studio/capacitor-google-auth^3.2
```

执行您的工程指令 [从电容 3 更新到电容 4](https://capacitorjs.com/docs/updating/4-0)。 。 。 .

#### 从 3. 0.2 移到 3.1.0

```diff
- GoogleAuth.init()
+ GoogleAuth.initialize()
```

#### 从2个移到3个

安装3.x.x版本 :

```sh
npm i --save @codetrix-studio/capacitor-google-auth^3.0
```

之后 [迁移到 Capcitor 3](https://capacitorjs.com/docs/updating/3-0) 更新项目,参见 diff :

##### 韦伯

```diff
- import "@codetrix-studio/capacitor-google-auth";
- import { Plugins } from '@capacitor/core';
+ import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth'

- Plugins.GoogleAuth.signIn();
+ GoogleAuth.init()
+ GoogleAuth.signIn()
```

#### 从1移到2

安装 2. x. x 版本 :

```sh
npm i --save @codetrix-studio/capacitor-google-auth@2
```

2.x.x 用于电容器 [指令](https://github.com/CodetrixStudio/CapacitorGoogleAuth/blob/79129ab37288f5f5d0bb9a568a95890e852cebc2/README.md)

## 许可证

[麻省理工学院](https://npmcn.com/package/%40codetrix-studio/capacitor-google-auth/LICENSE)

## 常见问题

### @codetrix-studio/capacitor-google-auth 是什么？

电容器的Google Auth插件\.

### 如何安装 @codetrix-studio/capacitor-google-auth？

在项目目录运行 npm install @codetrix-studio/capacitor-google-auth，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@capacitor/core（^6\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @codetrix-studio/capacitor-google-auth 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @codetrix-studio/capacitor-google-auth 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @codetrix-studio/capacitor-google-auth 可以商用吗？

@codetrix-studio/capacitor-google-auth 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @codetrix-studio/capacitor-google-auth 的下载量有多少？

根据 npm 官方下载统计，@codetrix-studio/capacitor-google-auth 在 2026/10/10 – 2026/11/08 这 30 天内被下载 223,599 次，日均约 7,453 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @codetrix-studio/capacitor-google-auth 还在维护吗？

最新版本 3\.4\.0-rc\.4 发布于 2024/05/01（2 年前）；至今共发布 44 个版本，其中过去 12 个月发布了 0 个；首次发布于 2019/05/20。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

