# 谷歌单点登录提供商

> Source: https://strapi.nodejs.cn/cms/configurations/sso-providers/google

🌐 Google provider SSO configuration

在你的 Strapi 管理面板中将 Google 配置为 SSO 提供商，以允许用户使用其 Google 账户凭据登录和注册。

🌐 Configure Google as an SSO provider in your Strapi admin panel to allow users to sign in and sign up using their Google account credentials.

本页面说明了如何为[单点登录 (SSO) 功能](/cms/features/sso) 设置 Google 提供商。

🌐 The present page explains how to setup the Google provider for the [Single Sign-On (SSO) feature](/cms/features/sso).

:::prerequisites

你已阅读[如何配置 SSO 指南](/cms/configurations/guides/configure-sso)。

🌐 You have read the [How to configure SSO guide](/cms/configurations/guides/configure-sso).

:::

## 安装 {#installation}

🌐 Installation

安装 [passport-google-oauth2](https://github.com/mstade/passport-google-oauth2)：

```sh
yarn add passport-google-oauth2
```

```sh
npm install --save passport-google-oauth2
```

## 配置示例 {#configuration-example}

🌐 Configuration example

Google SSO 提供程序在 [`config/admin` 文件](/cms/configurations/admin-panel) 的 `auth.providers` 数组中配置：

🌐 The Google SSO provider is configured in the `auth.providers` array of [the `config/admin` file](/cms/configurations/admin-panel):

```js title="/config/admin.js"

const GoogleStrategy = require("passport-google-oauth2");

module.exports = ({ env }) => ({
  auth: {
    // ...
    providers: [
      {
        uid: "google",
        displayName: "Google",
        icon: "https://cdn2.iconfinder.com/data/icons/social-icons-33/128/Google-512.png",
        createStrategy: (strapi) =>
          new GoogleStrategy(
            {
              clientID: env("GOOGLE_CLIENT_ID"),
              clientSecret: env("GOOGLE_CLIENT_SECRET"),
              scope: [
                "https://www.googleapis.com/auth/userinfo.email",
                "https://www.googleapis.com/auth/userinfo.profile",
              ],
              callbackURL:
                strapi.admin.services.passport.getStrategyCallbackURL("google"),
            },
            (request, accessToken, refreshToken, profile, done) => {
              done(null, {
                email: profile.email,
                firstname: profile.given_name,
                lastname: profile.family_name,
              });
            }
          ),
      },
    ],
  },
});
```

```ts title="/config/admin.ts"

  auth: {
    // ...
    providers: [
      {
        uid: "google",
        displayName: "Google",
        icon: "https://cdn2.iconfinder.com/data/icons/social-icons-33/128/Google-512.png",
        createStrategy: (strapi) =>
          new GoogleStrategy(
            {
              clientID: env("GOOGLE_CLIENT_ID"),
              clientSecret: env("GOOGLE_CLIENT_SECRET"),
              scope: [
                "https://www.googleapis.com/auth/userinfo.email",
                "https://www.googleapis.com/auth/userinfo.profile",
              ],
              callbackURL:
                strapi.admin.services.passport.getStrategyCallbackURL("google"),
            },
            (request, accessToken, refreshToken, profile, done) => {
              done(null, {
                email: profile.email,
                firstname: profile.given_name,
                lastname: profile.family_name,
              });
            }
          ),
      },
    ],
  },
});
```
