let Google_appId = "*****.apps.googleusercontent.com";
// Called when Google Javascript API Javascript is loaded
function HandleGoogleApiLibrary() {
// Load "client" & "auth2" libraries
gapi.load('client:auth2', {
callback: function () {
// Initialize client & auth libraries
gapi.client.init({
clientId: Google_appId,
scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me'
}).then(
function (success) {
// Google Libraries are initialized successfully
// You can now make API calls
console.log("Google Libraries are initialized successfully");
},
function (error) {
// Error occurred
console.log(error);// to find the reason
}
);
},
onerror: function () {
// Failed to load libraries
console.log("Failed to load libraries");
}
});
}
function GoogleLogin() {
// API call for Google login
gapi.auth2.getAuthInstance().signIn().then(
function (success) {
// Login API call is successful
console.log(success);
let Google_ID = success["El"];
//這邊可以寫抓到登入資訊後要做的事,比如將會員資料寫到資料庫之類的
},
function (error) {
// Error occurred
// console.log(error) to find the reason
console.log(error);
}
);
}
接著就可以這樣拿到 google 登入後的資料了。
// API call to get user profile information
gapi.client.request({ path: 'https://www.googleapis.com/plus/v1/people/me' }).then(
function(success) {
// API call is successful
var user_info = JSON.parse(success.body);
// user profile information
console.log(user_info);
},
function(error) {
// Error occurred
// console.log(error) to find the reason
}
);
留言
如果要示範實作麻煩什麼時候用fetch寫清楚點
signIn()有沒有效也請事前檢查清楚