Google 登录流程梳理 [前端与http层]

前台存放项目的client_id和client_secret(从Google官方申请下来的)

同时项目引入gapi包,在全局种下gapi变量

 

调用全局gapi.auth2.init方法,传入client_id,获取auth2对象。

对auth2对象调用grantOfflineAccess()方法,页面弹出Google登录框。

用户在页面上输入登录账号密码回车,触发grantOfflineAccess()方法的promise回调,resolve里返回code,形如

{code: "4/pAFQK3NDf9Ahu-8-HH11TadgVTC-gGYJPoR-NcG_ylNrbkcAKFU9nZtyKCxLuwKy_Wz7Ow69WO8n2tBhU0XvOiA”}

将code作为参数,redirectUrl作为header,向http层发起Login请求

————————————————————————————————————

http层收到前台传来的redirectUrl和code

引入google-auth-library 调用 OAuth2Client方法,传入后台存的同一套client_id、client_secret,以及redirectUrl,new一个oAuth2Client,得到形如下的对象

oAuth2Client{
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  transporter: DefaultTransporter {},
  credentials: {
    access_token: 'ya29.GltmB6QfWFAFkl...',
    refresh_token: '1/tQ_8vuUvp2astHuOUjMbl3...',
    scope: 'openid ' +
      'https://www.googleapis.com/auth/userinfo.profile ' +
      'https://www.googleapis.com/auth/userinfo.email',
    token_type: 'Bearer',
    id_token: 'eyJhbGciOiJSUzI1...',
    expiry_date: 1566198602524
  },
  certificateCache: {},
  certificateExpiry: null,
  certificateCacheFormat: 'PEM',
  refreshTokenPromises: Map {},
  _clientId: '490136655706-ljvb5j...del9kqum.apps.googleusercontent.com',
  _clientSecret: 'qjzBLgt2UdJvA...',
  redirectUri: 'http://localhost:8081',
  eagerRefreshThresholdMillis: 300000
}

从credentials的字段里取出refresh_token,返回它给到前端。

(本文只讨论Google登录流程。至于前端拿refresh_token怎么用,存在哪儿,以及后台存下refresh_token后的过期操作,不在本文讨论范围内。)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值