vue實現(xiàn)google授權(quán)登錄獲取信息

前提:vue環(huán)境,google授權(quán)登錄需要翻墻。

一、google賬號申請+創(chuàng)建憑據(jù)
地址:
https://console.developers.google.com/apis/credentials?project=matest-247702

申請完賬號之后進入

1587613566.jpg

點擊創(chuàng)建憑據(jù)


1587613696(1).jpg

這里我添加的url是在本地測試的,授權(quán)的重定向url是google通過驗證后重定向的地址,我填的也是本地url,添加完之后點擊保存就會生成一個客戶端 ID,代碼中需要用到。

二、代碼
安裝依賴:
cnpm install --save vue-google-signin-button
并在main.js引入:
import GSignInButton from 'vue-google-signin-button'
Vue.use(GSignInButton)

index.html文件引入
<script async defer crossorigin="anonymous" src="https://apis.google.com/js/api:client.js"></script>

需要授權(quán)的頁面
html:
<g-signin-button
:params="googleSignInParams"
@success="onSignInSuccess"
@error="onSignInError">
Google
</g-signin-button>

<script>
data() {
return {
googleSignInParams: {
client_id: '106825946812-7o529upnhp5emn87ronp5vtr72ipcps3.apps.googleusercontent.com'
} //客戶端Id
}
}

//google授權(quán)登錄
onSignInSuccess (googleUser) {
const info = googleUser.getBasicProfile() //獲取信息
const name = info.getName(); // 姓名
const id = info.getId(); // id
const givenName = info.getGivenName(); // 名
const familyName = info.getFamilyName(); // 姓
const imageUrl = info.getImageUrl(); // imageUrl
const email = info.getEmail(); // email
console.log(info,name,id,givenName,familyName,imageUrl,email)
},
onSignInError(error) {
console.log(error)
}

</script>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS13中,如果蘋果開發(fā)者提供任何其他第三方登錄,就必須提供“蘋果登錄”選項。也就是說,如果軟件要求“微信登錄...
    抓魚貓L閱讀 22,953評論 16 19
  • 級別:★☆☆☆☆標簽:「iOS 接入 Google、Facebook 登錄」「Firebase Google」「F...
    QiShare閱讀 8,491評論 6 11
  • 技術(shù)無極限,從菜鳥開始,從源碼開始。 由于公司目前項目還是用OC寫的項目,沒有升級swift 所以暫時SDWebI...
    充滿活力的早晨閱讀 12,858評論 0 2
  • IOS常用代碼總結(jié) 1、設置UILabel行間距 NSMutableAttributedString* attrS...
    難卻卻閱讀 649評論 0 0
  • 研究背景 一般用于描述 font-size、padding 或者 border 的長度都使用 px 單位。但最近看...
    onizuka_jp閱讀 461評論 0 0

友情鏈接更多精彩內(nèi)容