Google 最近向我发送了一封电子邮件,其中包含以下内容:
您的一个或多个 Web 应用程序使用旧版 Google 登录 JavaScript 库。请在 2023 年 3 月 31 日之前将您的项目迁移到新的 Google Identity Services SDK
该项目使用 Google Drive API 以及现在的旧版身份验证客户端。
迁移页面( https://developers.google.com/identity/gsi/web/guides/migration )上的表格显示:
| 老的 | 新的 | 笔记 |
|---|---|---|
| JavaScript 库 | ||
| apis.google.com/js/platform.js | account.google.com/gsi/client | 以新换旧。 |
| apis.google.com/js/api.js | account.google.com/gsi/client | 以新换旧。 |
我目前正在gapi前端使用来执行从apis.google.com/js/api.js. 根据该表,我需要将其替换为新库。
我尝试了以下方法来进行身份验证和授权,其方式与我以前对gapi进行的操作相同:
window.google.accounts.id.initialize({
client_id: GOOGLE_CLIENT_ID,
callback: console.log,
scope: "https://www.googleapis.com/auth/drive.file",
discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"],
});
window.google.accounts.id.renderButton(ref.current, {
size: "medium",
type: "standard",
});
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用 Google 登录按钮进行身份验证时,该scope字段不受尊重,并且不会要求用户授权所请求的范围。它也不会在回调的凭据响应中返回任何形式的访问令牌。
我不知道如何授权使用新库。
javascript google-api google-api-client google-oauth google-identity
在我的 CI 中,我需要确保我的代码与 mongo 一起使用,因此我使用官方 mongo docker 映像并将所需的凭据作为 mongo 映像的环境变量传递。
build-on-mongo:
runs-on: ${{ matrix.os }}
env:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: vote-stuysu-org
services:
mongo:
image: mongo
ports:
# Maps tcp port 27017 on service container to the host
- 27017:27017
strategy:
matrix:
os: [ubuntu-20.04]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '14.x'
- name: Install dependencies
run: npm install
- name: Test
run: npm test
env:
MONGO_URL: mongodb://root:password@localhost:27017/vote-stuysu-org
Run Code Online (Sandbox Code Playgroud)
但是,在测试步骤中,会记录一条错误消息:
(节点:2158)未处理的PromiseRejectionWarning:MongoError:身份验证失败。
我在 nodejs 旁边使用 mongoose,所以这是负责身份验证的代码:
mongoose.connect(process.env.MONGO_URL, {
useUnifiedTopology: true, …Run Code Online (Sandbox Code Playgroud)