Google以编程方式登录

问题描述 投票:0回答:1

我正在创建REST后端(node.js,虽然对于这个问题并不重要),我通过Google登录对用户进行身份验证。

这就是auth和创建用户端点的方式在swagger中(为简单起见,我删除了所有其他部分):

openapi: 3.0.0
info:
  version: 0.1.0
  title: XXX API
servers:
  - url: 'https://xxx.herokuapp.com/'
  - url: 'http://localhost:5000/'
paths:
  /auth:
    post:
      summary: Authenticate user
      operationId: authUser
      tags:
        - auth
      parameters:
        - name: googleToken
          in: query
          required: true
          schema:
            $ref: '#/components/schemas/GoogleToken'
      responses:
        '200':
          $ref: '#/components/responses/LoggedUser'
  /users:
    post:
      summary: Create new user
      operationId: createUser
      tags:
        - users
      parameters:
        - name: googleToken
          in: query
          required: true
          schema:
            $ref: '#/components/schemas/GoogleToken'
      responses:
        '201':
          $ref: '#/components/responses/LoggedUser'
components:
  schemas:
    UserId:
      type: string
    GoogleToken:
      type: string
    User:
      type: object
      properties:
        userId:
          $ref: '#/components/schemas/UserId'
  responses:
    LoggedUser:
      description: Successfully logged user
      headers:
        Auth-Token:
          schema:
            type: string
            example: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJP
      content:
        application/json:
          schema:
            allOf:
              - $ref: '#/components/schemas/User'

如您所见,我依靠Google令牌进行身份验证和用户创建。根据文档的建议,我使用谷歌令牌来验证用户并检索它的谷歌ID,电子邮件等,而不是发送普通的googleIds。

为了保护我的API免受开发期间的意外故障的影响,我通常会通过Soap UI或Assertible等服务创建一些功能测试。在那里,您可以列出您的端点以及您对给定参数的期望响应。

我面临的问题是,如何通过我的测试以编程方式模拟Google登录? Facebook有创建测试用户link的功能,但显然我无法在Google Docs中找到相同的解决方案。当然,我可以获得一个Google令牌并对其进行硬编码,但它的到期时间相当短(几个小时)。

node.js谷歌库中有一个线程,但没有适当的解决方案:github thread

任何关于如何绕过这个问题的建议都非常感谢。

问候,

google-oauth google-oauth2 google-openid
1个回答
0
投票

为您的测试帐户申请刷新令牌。然后使用它在每次测试运行开始时请求访问令牌。见How do I authorise an app (web or installed) without user intervention?

© www.soinside.com 2019 - 2024. All rights reserved.