Vue實(shí)現(xiàn)Google第三方登錄的示例代碼
一、開(kāi)發(fā)者平臺(tái)配置
1、進(jìn)入開(kāi)發(fā)者平臺(tái),首先前往Google API 控制臺(tái)選擇或者創(chuàng)建一個(gè)項(xiàng)目
一堆眼花繚亂的API讓你無(wú)從選擇,但是你只要記住這次進(jìn)來(lái)的目的是:社交API
2.使用這個(gè)API之前還需要做一件事,那就是申請(qǐng)一個(gè)OAuth 2.0 客戶端 ID
3按照要求填寫你項(xiàng)目的類型、名稱以及來(lái)源url
注:創(chuàng)建完成之后,頁(yè)面也有一個(gè)彈窗將你申請(qǐng)的客戶端ID已經(jīng)密鑰展示出來(lái),沒(méi)錯(cuò)這個(gè)就是一個(gè)生成的過(guò)程。
4、安裝vue-google-signin-button
npm install vue-google-signin-button --save
5、在main.js中引入并注冊(cè)
import GSignInButton from 'vue-google-signin-button' Vue.use(GSignInButton);
6.index.html引入js文件
<!--谷歌登錄需要的依賴js--> <script src="http://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>
解決問(wèn)題BUG
1、問(wèn)題一:初始化沒(méi)有引入js
你會(huì)發(fā)現(xiàn)在初始化的時(shí)候頁(yè)面會(huì)出現(xiàn)一個(gè)報(bào)錯(cuò)。
出現(xiàn)這個(gè)問(wèn)題的原因就是插件本身是沒(méi)有引入Google.js文件。解決辦法就是Vue的index.html中引入,詳情看下圖。
到此這篇關(guān)于Vue實(shí)現(xiàn)Google第三方登錄的示例代碼的文章就介紹到這了,更多相關(guān)Vue Google第三方登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于vue-cli配置lib-flexible + rem實(shí)現(xiàn)移動(dòng)端自適應(yīng)
這篇文章主要介紹了基于vue-cli配置lib-flexible + rem實(shí)現(xiàn)移動(dòng)端自適應(yīng),需要的朋友可以參考下2017-12-12vue App.vue中的公共組件改變值觸發(fā)其他組件或.vue頁(yè)面監(jiān)聽(tīng)
這篇文章主要介紹了vue App.vue里的公共組件改變值觸發(fā)其他組件或.vue頁(yè)面監(jiān)聽(tīng),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05