Vue如何实现Google第三方登录(Google,vue,web开发)

时间:2024-04-28 11:10:04 作者 : 石家庄SEO 分类 : web开发
  • TAG :

一、开发者平台配置

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

npminstallvue-google-signin-button--save

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

importGSignInButtonfrom"vue-google-signin-button"Vue.use(GSignInButton);

6.index.html引入js文件

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

7、在login.vue中使用组件

<template><g-signin-button:params="googleSignInParams"@success="onSignInSuccess"@error="onSignInError">SigninwithGoogle</g-signin-button></template><script>exportdefault{data(){return{/***TheAuth2parameters,asseenon*https://developers.google.com/identity/sign-in/web/reference#gapiauth2initparams.*Astheveryleast,avalidclient_idmustpresent.*@type{Object}*/googleSignInParams:{client_id:"YOUR_APP_CLIENT_ID.apps.googleusercontent.com"}}},methods:{onSignInSuccess(googleUser){console.log(googleUser)constprofile=googleUser.getBasicProfile()console.log(profile)},onSignInError(error){console.log("OHNOES",error)}}}</script><style>.g-signin-button{/*Thisiswhereyoucontrolhowthebuttonlooks.Becreative!*/display:inline-block;padding:4px8px;border-radius:3px;background-color:#3c82f7;color:#fff;box-shadow:03px0#0f69ff;}</style>

Vue如何实现Google第三方登录

解决问题BUG

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

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

Vue如何实现Google第三方登录

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

Vue如何实现Google第三方登录

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Vue如何实现Google第三方登录的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:HTML如何实现一个居中排列的标题下一篇:

17 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18