首先放个按钮,当然可以用谷歌自己的,这个更简单。我这里使用了自己的按钮
<div id="google-login">
<img src="{{ URL::asset('images/common/share_google.png') }}" />
</div>
引入js
<script src="https://apis.google.com/js/api:client.js"></script>
js获取授权
var googleUser = {};
var startApp = function() {
gapi.load('auth2', function(){
// Retrieve the singleton for the GoogleAuth library and set up the client.
auth2 = gapi.auth2.init({
client_id: 'xxxxxx.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
//scope: 'additional_scope'
});
attachSignin(document.getElementById('google-login'));
});
};
function attachSignin(element) {
console.log(element.id);
auth2.attachClickHandler(element, {},
function(googleUser) {
var profile = googleUser.getBasicProfile();
var idToken = googleUser.getAuthResponse().id_token;
var name = profile.getName();
var email = profile.getEmail();
var avatar = profile.getImageUrl();
}, function(error) {
console.log(error)
});
}
startApp();