Vue實(shí)現(xiàn)Google第三方登錄的示例代碼
1、進(jìn)入開發(fā)者平臺,首先前往Google API 控制臺選擇或者創(chuàng)建一個項(xiàng)目
谷歌開發(fā)者平臺
一堆眼花繚亂的API讓你無從選擇,但是你只要記住這次進(jìn)來的目的是:社交API
2.使用這個API之前還需要做一件事,那就是申請一個OAuth 2.0 客戶端 ID
3按照要求填寫你項(xiàng)目的類型、名稱以及來源url
注:創(chuàng)建完成之后,頁面也有一個彈窗將你申請的客戶端ID已經(jīng)密鑰展示出來,沒錯這個就是一個生成的過程。
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='https://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>
1、問題一:初始化沒有引入js
你會發(fā)現(xiàn)在初始化的時(shí)候頁面會出現(xiàn)一個報(bào)錯。
出現(xiàn)這個問題的原因就是插件本身是沒有引入Google.js文件。解決辦法就是Vue的index.html中引入,詳情看下圖。
到此這篇關(guān)于Vue實(shí)現(xiàn)Google第三方登錄的示例代碼的文章就介紹到這了,更多相關(guān)Vue Google第三方登錄內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JSP之表單提交get和post的區(qū)別詳解及實(shí)例2. CSS可以做的幾個令你嘆為觀止的實(shí)例分享3. Xml簡介_動力節(jié)點(diǎn)Java學(xué)院整理4. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲5. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁6. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案7. jsp文件下載功能實(shí)現(xiàn)代碼8. 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實(shí)現(xiàn)方法9. ASP常用日期格式化函數(shù) FormatDate()10. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能
