Vue实现Google第三方登录的示例代码

2022-04-15 0 698
目录
  • 一、开发者平台配置
  • 解决问题BUG

一、开发者平台配置

1、进入开发者平台,首先前往Google API 控制台选择或者创建一个项目

谷歌开发者平台

Vue实现Google第三方登录的示例代码

一堆眼花缭乱的API让你无从选择,但是你只要记住这次进来的目的是:社交API

Vue实现Google第三方登录的示例代码

2.使用这个API之前还需要做一件事,那就是申请一个OAuth 2.0 客户端 ID

Vue实现Google第三方登录的示例代码

3按照要求填写你项目的类型、名称以及来源url

注:创建完成之后,页面也有一个弹窗将你申请的客户端ID已经密钥展示出来,没错这个就是一个生成的过程。

Vue实现Google第三方登录的示例代码

4、安装vue-google-signin-button

npm install vue-google-signin-button --save

5、在main.js中引入并注册

import GSignInButton from 'vue-google-signin-button'
Vue.use(GSignInButton);

6.index.html引入js文件

<!--谷歌登录需要的依赖js-->
<script src="//apis.google.com/js/api:client.js"></script>

7、在login.vue中使用组件

<template>
  <g-signin-button
    :params="googleSignInParams"
    @success="onSignInSuccess"
    @error="onSignInError">
    Sign in with Google
  </g-signin-button>
</template>

<script>
export default {
  data () {
    return {
      /**
       * The Auth2 parameters, as seen on
       * https://developers.google.com/identity/sign-in/web/reference#gapiauth2initparams.
       * As the very least, a valid client_id must present.
       * @type {Object}
       */
      googleSignInParams: {
        client_id: 'YOUR_APP_CLIENT_ID.apps.googleusercontent.com'
      }
    }
  },
  methods: {
    onSignInSuccess (googleUser) {
      console.log(googleUser)
      const profile = googleUser.getBasicProfile()
      console.log(profile)
    },
    onSignInError (error) {
      console.log('OH NOES', error)
    }
  }
}
</script>

<style>
.g-signin-button {
  /* This is where you control how the button looks. Be creative! */
  display: inline-block;
  padding: 4px 8px;
  border-radius: 3px;
  background-color: #3c82f7;
  color: #fff;
  box-shadow: 0 3px 0 #0f69ff;
}
</style>

Vue实现Google第三方登录的示例代码

解决问题BUG

1、问题一:初始化没有引入js

你会发现在初始化的时候页面会出现一个报错。

Vue实现Google第三方登录的示例代码

出现这个问题的原因就是插件本身是没有引入Google.js文件。解决办法就是Vue的index.html中引入,详情看下图。

Vue实现Google第三方登录的示例代码

到此这篇关于Vue实现Google第三方登录的示例代码的文章就介绍到这了,更多相关Vue Google第三方登录内容请搜索NICE源码以前的文章或继续浏览下面的相关文章希望大家以后多多支持NICE源码!

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript Vue实现Google第三方登录的示例代码 https://www.niceym.com/29450.html