亲宝软件园·资讯

展开

vue谷歌授权登录获取通讯录

slc6666 人气:0

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

业务背景

   // 初始化谷歌授权登录
    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.auth2
              .getAuthInstance()
              .isSignedIn.listen(that.updateSigninStatus)

            // Handle the initial sign-in state.
            // that.updateSigninStatus(
            //   gapi.auth2.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.auth2.getAuthInstance().isSignedIn.get()
      if (flag) {
        // 如果已经登录,就直接弹出窗口
        this.listConnectionNames()
      } else {
        // 未登录就调用出登录授权
        gapi.auth2.getAuthInstance().signIn()
      }
    },
    /**
     *  Sign out the user upon button click.
     */
    handleSignoutClick(event) {
      gapi.auth2.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:auth2', that.initClient)
     },

加载全部内容

相关教程
猜你喜欢
用户评论