简体   繁体   English

Node.js 脚本中的 Google 登录

[英]Google sign-in inside Node.js script

I want to integrate google sign-in in my Node.js script so I can log who used that script.我想在我的 Node.js 脚本中集成谷歌登录,这样我就可以记录谁使用了该脚本。
I am able to add google sign-in in webpage with help of https://developers.google.com/identity/sign-in/web/sign-in but could not find way to add google sign-in in standlaone script.我可以在https://developers.google.com/identity/sign-in/web/sign-in的帮助下在网页中添加谷歌登录,但找不到在独立脚本中添加谷歌登录的方法。

Let me try explain my intention让我试着解释一下我的意图

var googleSignIn = async function () {
    // sign in using google account and return email addess on succesfull sign in
}

var work = async function () {
    // step 1: log in first using google
    var email = await googleSignIn()

    // step 2: talk to backed server api on succesfull login
    if (email) {
        console.log('calling backend api')
    } else {
        console.log('you need to login first')
    }
}

work().then(function () {
    console.log('done')
}).catch(function (error) {
    throw error
})

I want to implement googleSignIn method.我想实现googleSignIn方法。 How can I do that?我怎样才能做到这一点?

Your script would need to operate in the same manner as a desktop app, and perform these actions:您的脚本需要以与桌面应用程序相同的方式运行,并执行以下操作:

  • Create an authorization redirect URL in terms of Authorization Code Flow (PKCE)根据授权代码流 (PKCE) 创建授权重定向 URL
  • Open the system browser, which will manage redirects and the user login打开系统浏览器,它将管理重定向和用户登录
  • Receive the response in your app, via either a Loopback or Private URI Scheme notification通过 Loopback 或 Private URI Scheme 通知在您的应用中接收响应
  • The response contains an authorization code, which needs to be swapped for tokens响应包含一个授权码,需要交换令牌
  • You can then call your API with an access token然后,您可以使用访问令牌调用您的 API

A typical URL would be a value like this:典型的 URL 将是这样的值:

https://accounts.google.com/oauth2/v2.0/authorize?
client_id=y792f434f
&response_type=code
&redirect_uri=com.mycompany.myapp:/callback
&scope=...
&state=...
&code_challenge=...
&code_challenge_method=S256

It is a tricky flow to implement though - in case useful there are further details in my Desktop OAuth Blog Posts , and code samples you can run...虽然这是一个棘手的流程 - 如果有用,我的桌面 OAuth 博客帖子中有更多详细信息,您可以运行代码示例......

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM