简体   繁体   English

如何通过一键登录流程使用适用于 JavaScript 的 Google API 客户端?

[英]How to use the Google API client for JavaScript with a One-Tap sign in flow?

I'm using Google One-Tap sign in to authenticate users, and after the user is authenticated I get an access token.我正在使用Google One-Tap 登录对用户进行身份验证,在用户通过身份验证后,我会获得一个访问令牌。 I know that I can use this access token in order to work with the Google API client for JavaScript ("GAPI").我知道我可以使用此访问令牌来使用JavaScriptGoogle API 客户端(“GAPI”)。 But I can't find any way to work with GAPI using this access token.但是我找不到任何使用此访问令牌使用 GAPI 的方法。

Is there any way to use GAPI assuming I already have an logged in user?假设我已经有一个登录用户,有没有办法使用 GAPI?

What I'm trying to do is access the user calendar after simply authenticating with One-Tap authentication and giving consent for the calendar once.我想要做的是在简单地通过一键式身份验证进行身份验证并同意日历一次后访问用户日历。

First of all:首先:
There is no way to "authenticate" the google JS api client with the response that is returned by the One-Tap sign in.无法使用 One-Tap 登录返回的响应来“验证”google JS api 客户端。

Luckily we don't need to authenticate with the gapi JS client because we use a handy function called gapi.auth2.authorize !幸运的是,我们不需要使用gapi.auth2.authorize JS 客户端进行身份验证,因为我们使用了一个名为gapi.auth2.authorize的方便函数!

How to authorize the gapi client如何授权gapi客户端

It's important to first read the docs and understand their warning:首先阅读文档并理解他们的警告很重要:

Do not use this method alongside the recommended gapi.auth2.init and signIn flow.不要将此方法与推荐的 gapi.auth2.init 和 signIn 流程一起使用。 These are two distinct behaviors (Authorization for gapi.auth2.authorize vs Authentication for gapi.auth2.init/signIn) and will have unexpected issues if used within the same application.这是两种不同的行为(gapi.auth2.authorize 的授权与 gapi.auth2.init/signIn 的身份验证),如果在同一应用程序中使用,则会出现意外问题。

Now the question is how to completely avoid the init/signIn method.现在的问题是如何完全避免 init/signIn 方法。

Step 1第 1 步
Sign the user into the Google One-Tap sign in.让用户登录 Google One-Tap 登录。

Step 2第 2 步
Load the gapi client: window.gapi.load('client')加载gapi客户端: window.gapi.load('client')

Step 3第 3 步
Pass the credential.id (the email address) returned by Google One-Tap as the login_hint param in the call to authorize.将 Google One-Tap 返回的credential.id (电子邮件地址)作为调用中的login_hint参数进行授权。 This will preselect the account and we can try to not show any new login pop-up (prompt).这将预先选择帐户,我们可以尝试不显示任何新的登录弹出窗口(提示)。

Example:示例:

gapi.auth2.authorize({
  client_id,
  prompt: 'none',
  response_type: 'permission', // Access Token.
  scope: 'CALENDAR_SCOPE',
  login_hint: credential.id
}, function(result) {})

Using prompt: 'none', you can try to fetch a token without any UI.使用 prompt: 'none',您可以尝试在没有任何 UI 的情况下获取令牌。 This is useful to check whether you need to show an Authorize button, and also useful before making any call to the Calendar API to get a fresh token.这对于检查您是否需要显示授权按钮很有用,并且在调用 Calendar API 以获取新令牌之前也很有用。

Step 4第 4 步
Before we can make any call to gapi.client.calendar we need to initialize the client with just the calendar discoveryDocs .在我们可以对gapi.client.calendar进行任何调用gapi.client.calendar我们需要仅使用日历discoveryDocs初始化client

gapi.client.init({discoveryDocs})

This is the most tricky part and is not properly documented anywhere!这是最棘手的部分,在任何地方都没有正确记录! The only thing we can retrieve from the api.client.init() documentation is the following line:我们可以从api.client.init() 文档中检索到的api.client.init() 内容是以下行:

If OAuth client ID and scope are provided, this function will load the gapi.auth2 module to perform OAuth如果提供了 OAuth 客户端 ID 和范围,此函数将加载 gapi.auth2 模块以执行 OAuth

This basically means: as soon as you give either clientID or scope gapi.client.init will try and start the traditional authentication method.这基本上意味着:只要您提供clientIDscope gapi.client.init将尝试启动传统的身份验证方法。
In our case: we don't need to pass the clientID or scope as we've already done this in step 3 .在我们的例子中:我们不需要传递clientIDscope因为我们已经在第 3 步中完成了。

So how does the client know which module to initialize?那么客户端怎么知道要初始化哪个模块呢? → By only passing the discoveryDocs of the module you want to use! → 只传递您要使用的模块的 discoveryDocs! In this case the calendar discoveryDocs.在这种情况下,日历发现文档。

Step 5第 5 步
Now you're done!现在你完成了! You can make requests with eg gapi.client.calendar.events.list()您可以使用例如gapi.client.calendar.events.list()提出请求


Full example完整示例

A full code example can be found here below:可以在下面找到完整的代码示例:

const config =  {
  response_type: 'permission',
  scope: 'CALENDAR_SCOPE',
  client_id: clientId,
  login_hint: credential.id,
  promt: 'none',
}
gapi.auth2.authorize(config, function(response) {
  // No need to `setToken`, it's done for you by auth2.
  let calConfig = {discoveryDocs} // only of google calendar
  window.gapi.client.init(calConfig).then(function() {
    // then execute a calendar call:
    window.gapi.client.calendar.events.list({'calendarId': 'primary'})
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署后不显示 Google One-Tap 登录弹出窗口 - Google One-Tap Sign in popup doesn't show up after deployment 使用 SMS 用户同意的 Google 一键式 SMS 验证 API 错误:状态{statusCode=SUCCESS, resolution=null} - Google One-tap SMS verification with the SMS User Consent API Error: Status{statusCode=SUCCESS, resolution=null} Google One-Tap如何管理我的刷新令牌? 它与GAPI有何不同? - How does Google One-Tap manage my refresh tokens? How does it differ from GAPI? 登入Google API用户端时发生IllegalStateException - IllegalStateException on sign to Google api client 如何管理Google登录会话(Google登录JavaScript客户端) - How to manage google sign-in session (Google Sign-In JavaScript client) 将 Google 服务帐户与 google API JavaScript 客户端一起使用 - Use Google Service Account with google API JavaScript Client 如何在Google Calendar API中使用run_flow()? - How to use run_flow() in Google Calendar API? Google javascript登录API:无法离线访问 - Google javascript sign-in api: no offline access Google API OAuth 2 登录新的 OAuth 2 客户端出现问题 - Google API OAuth 2 sign in something went wrong with new OAuth 2 client .NET Google Api客户端您无法注册Google Apps,因为您的一个或多个订阅处于暂停状态 - .NET Google Api Client You cannot sign up for Google Apps because one or more of your subscriptions are in a suspended state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM