9

我正在创建一个与 Node.js 服务器通信的 Android/iOS 应用程序,并希望使用 Google(和/或 Facebook)和 OAuth2 在我的服务器上安全地识别它们。我查看了以下文档:https ://developers.google.com/+/web/signin/server-side-flow

我不需要授权,我只需要身份验证(我只想确保调用我的 Node.js 服务的人就是他们所说的那个人)。为此,如果我理解正确,我必须让用户在客户端使用 Google 登录,这将为他们提供一个授权码,然后他们可以将其提供给我的服务器。然后,我的服务器可以将该代码交换为 access_token,从而检索有关用户的信息。然后我保证用户就是他们所说的那个人。

谷歌文档(上面的链接)说:“在授权重定向 URI 字段中,删除默认值。它不用于这种情况。”但是,为了让我的服务器将 authentication_code 交换为 access_token,它需要提供一个redirect_uri,我错过了什么吗?

例如,这redirect_uri对于 Unity 游戏是无用的(因为使用 Google 登录只会打开一个新的“窗口”,登录时会关闭,不涉及重定向)。

TL;DR 你如何使用 OAuth2 在我的客户端和我的服务器之间验证用户而无需重定向?

4

5 回答 5

5

TL;DR 你如何使用 OAuth2 在我的客户端和我的服务器之间验证用户而无需重定向?

你不能。OAuth 要求将用户定向到授权(可能还有登录)屏幕,然后重定向回您的应用程序。

于 2014-12-18T15:34:39.540 回答
5

你看过这个文档吗?https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi

选择重定向 URI

当您在 Google Developers Console 中创建客户端 ID 时,会为您创建两个 redirect_uris:urn:ietf:wg:oauth:2.0:oobhttp://localhost. 您的应用程序使用的值决定了授权代码如何返回到您的应用程序。

http://localhost

此值向 Google 授权服务器发出信号,授权代码应作为查询字符串参数返回给客户端上的 Web 服务器。您可以在不更改 Google Developers Console 配置的情况下指定端口号。要使用此 URL 接收授权代码,您的应用程序必须在本地 Web 服务器上进行侦听。这在许多但不是所有平台上都是可能的。如果您的平台支持,这是获取授权码的推荐机制。

于 2014-12-17T22:57:45.050 回答
2

redirect_uri可以是具有自定义 URL 方案的 URL,客户端为其注册了处理程序。此处对此进行了描述:什么是重定向 URI?它如何适用于 OAuth2.0 的 iOS 应用程序?. 与其说是“重定向”,不如说是关于应用程序的回调端点。

于 2015-01-07T18:58:56.973 回答
2

我遇到了这个问题,我花了很长时间才找到 Nepoxx 在此处接受的答案的评论中提到的“postmessage”解决方案。

为了澄清,这对我有用。

  1. 在此处执行步骤 1-6:https ://developers.google.com/identity/sign-in/web/server-side-flow
  2. 安装 googleapis 库npm install --save googleapis
  3. 对于服务器端令牌交换,请执行以下操作:
    var googleapis = require('googleapis');
    var OAuth2 = googleapis.auth.OAuth2;

    var oauth2Client = new OAuth2(
       GOOGLE_SSO_CLIENT_ID,
       GOOGLE_SSO_CLIENT_SECRET,
       'postmessage' // this is where you might otherwise specifiy a redirect_uri
    );

    oauth2Client.getToken(CODE_FROM_STEP_5_OF_INSTRUCTIONS, function(err, tokens) {
       // Now tokens contains an access_token and an optional refresh_token. Save them.
    });
于 2017-05-31T10:00:39.183 回答
1

如果您将 VueJS 与https://github.com/guruahn/vue-google-oauth2一起使用,这将变得非常容易

客户端

import GAuth from 'vue-google-oauth2'

Vue.use(GAuth, {
    clientId: 'xxxxxxx.apps.googleusercontent.com',
    scope: 'profile',
})
async signWithGoogle() {
    const code = await this.$gAuth.getAuthCode() //
    console.log(code ) // { code: 'x/xxxxxxxxxx' }
    // send the code to your auth server
    // and retrieve a JWT or something to keep in localstorage
    // to send on every request and compare with database
}

服务器端

import { google } from 'googleapis'

const oauth2Client = new google.auth.OAuth2(GOOGLE_ID, GOOGLE_SECRET, 'postmessage')

google.options({ auth: oauth2Client })

async function getAccount(code) {
    // the code you sent with the client
    const { tokens } = await oauth2Client.getToken(code)
    oauth2Client.setCredentials(tokens)
    const oauth2 = google.oauth2({ version: 'v2' })
    const { data: { id } } = await oauth2.userinfo.get()
    // there you have the id of the user to store it in the database
    // and send it back in a JWT
}
于 2019-04-18T15:25:35.730 回答