vue怎么调用谷歌授权登录获取用户通讯录

这篇文章主要讲解了“vue怎么调用谷歌授权登录获取用户通讯录”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue怎么调用谷歌授权登录获取用户通讯录”吧!调用谷歌授权,获取

这篇文章主要讲解了“vue怎么调用谷歌授权登录获取用户通讯录”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue怎么调用谷歌授权登录获取用户通讯录”吧!

调用谷歌授权,获取用户通讯录信息

业务背景

  • 业务端要求,用户本人填写信息,推荐到朋友,要求可以导出用户谷歌邮箱的通讯录,让用户选择,并且回显到页面 ##步骤

  • 在谷歌开发者平台 , 创建一个项目,我的理解是,我们的页面就是这个项目,要由我们的项目,对谷歌发起授权请求,就类似微信小程序,向官方发起授权,请求昵称和头像的这个场景,所以,后面我们的这个项目也要通过谷歌的审核。

  • 来到api服务

vue怎么调用谷歌授权登录获取用户通讯录

  • 这时候就到了我们这个项目的管理后台

  • 然后要创建一个用户凭据,拿到我们项目的id和key

vue怎么调用谷歌授权登录获取用户通讯录

  • 配置下面的域名,也就是让谷歌知道,用户从哪个域名发起请求事合理的,可以用本地localhost进行测试。不能用局域网IP

  • 然后在API库中,选择我们要用的API,我的需求是获取通讯录,所以我启用了people API

  • 在API库里,都会有用法和说明,我是自己去他的git上拉取的,看了下代码流程,然后自己改动,git上的代码很简洁

  • OAuth 同意屏幕 就是我们的应用在授权时,会跳出如下的界面

vue怎么调用谷歌授权登录获取用户通讯录

  • 我的理解就是这个屏幕就是同意屏幕,如果我们的应用没通过谷歌的验证,他就会提示我们的应用不安全。

  • 要想通过,这边有流程官方介绍

vue怎么调用谷歌授权登录获取用户通讯录

  • 我开发的时候,只是发布到正式了,没通过就是了。在开发环境没问题。

  • 然后就能拿到数据了。 全部的代码

   // 初始化谷歌授权登录
    initClient() {
      // Client ID and API key from the Developer Console
      let CLIENT_ID =
          '你申请的ID',
        API_KEY = '你申请的密码',
        // Array of API discovery doc URLs for APIs used by the quickstart
        DISCOVERY_DOCS = [
          'https://www.googleapis.com/discovery/v1/apis/people/v1/rest',
        ],
        // Authorization scopes required by the API; multiple scopes can be
        // included, separated by spaces.
        SCOPES = 'https://www.googleapis.com/auth/contacts.readonly',
        authorizeButton = document.getElementById('authorize_button'),
        that = this
      gapi.client
        .init({
          // apiKey: API_KEY,
          clientId: CLIENT_ID,
          discoveryDocs: DISCOVERY_DOCS,
          scope: SCOPES,
        })
        .then(
          function () {
            console.log('谷歌登录初始化成功')
            // Listen for sign-in state changes.
            gapi.auth3
              .getAuthInstance()
              .isSignedIn.listen(that.updateSigninStatus)

            // Handle the initial sign-in state.
            // that.updateSigninStatus(
            //   gapi.auth3.getAuthInstance().isSignedIn.get()
            // )
            authorizeButton.onclick = that.handleAuthClick
          },
          function (error) {
            console.log(error)
            // appendPre(JSON.stringify(error, null, 2))
          }
        )
    },
    /**
     *  Called when the signed in status changes, to update the UI
     *  appropriately. After a sign-in, the API is called.
     */
    updateSigninStatus(isSignedIn) {
      // 是否登录
      if (isSignedIn) {
        console.log('已登录')
        this.listConnectionNames()
      } else {
        console.log('未登录')
      }
    },
    /**
     *  Sign in the user upon button click.
     */
    handleAuthClick() {
      // 是否登录
      let flag = gapi.auth3.getAuthInstance().isSignedIn.get()
      if (flag) {
        // 如果已经登录,就直接弹出窗口
        this.listConnectionNames()
      } else {
        // 未登录就调用出登录授权
        gapi.auth3.getAuthInstance().signIn()
      }
    },
    /**
     *  Sign out the user upon button click.
     */
    handleSignoutClick(event) {
      gapi.auth3.getAuthInstance().signOut()
    },
    listConnectionNames() {
      let peopleMsg = [],
        that = this
      gapi.client.people.people.connections
        .list({
          resourceName: 'people/me',
          pageSize: 100,
          personFields: 'names,emailAddresses',
        })
        .then(function (response) {
          var connections = response.result.connections

          if (connections.length > 0) {
            for (let i = 0; i < connections.length; i++) {
              var person = connections[i]
              if (person.names && person.emailAddresses) {
                let obj = {
                  name: person.names[0].displayName,
                  email: person.emailAddresses[0].value,
                  id: i,
                }
                peopleMsg.push(obj)
              }
            }
          } else {
            that.$message({
              message: 'No connections found.',
              type: 'warning',
            })
          }

          that.peopleMsg = peopleMsg

          that.popDialog(peopleMsg)
        })
        .catch((err) => {
          console.log(err)
        })
    },
    
    
    // 在mounted的时候初始化一下窗口
    
     mounted() {
     // 谷歌登录授权初始化
        gapi.load('client:auth3', that.initClient)
     },

感谢各位的阅读,以上就是“vue怎么调用谷歌授权登录获取用户通讯录”的内容了,经过本文的学习后,相信大家对vue怎么调用谷歌授权登录获取用户通讯录这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是恰卡网,小编将为大家推送更多相关知识点的文章,欢迎关注!

本站部分文章来自网络或用户投稿,如无特殊说明或标注,均为本站原创发布。涉及资源下载的,本站旨在共享仅供大家学习与参考,如您想商用请获取官网版权,如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
后端

vue与react是什么框架

2022-7-16 9:08:15

后端

怎么使用Vue+canvas实现视频截图功能

2022-7-16 9:08:20

搜索