2017-01-23 74 views
0

如何在通過Google API登錄某人時檢查用戶是否具有默認圖像(例如,image.isDefault結果)。我使用getBasicProfile()檢索idnameemailimageUrl ...等Google API Javascript登錄 - 檢查用戶圖像是否爲默認

Code source

<html> 
<head> 
    <script src="https://apis.google.com/js/api:client.js"></script> 
    <script> 
    var googleUser = {}; 
    var startApp = function() { 
    gapi.load('auth2', function(){ 
     auth2 = gapi.auth2.init({ 
     client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com', 
     cookiepolicy: 'single_host_origin', 
     // Request scopes in addition to 'profile' and 'email' 
     //scope: 'additional_scope' 
     }); 
     attachSignin(document.getElementById('customBtn')); 
    }); 
    }; 

    function attachSignin(element) { 
    console.log(element.id); 
    auth2.attachClickHandler(element, {}, 
     function(googleUser) { 

     var profile = googleUser.getBasicProfile(); 

     var response = {}; 
     response.id =   profile.getId(); 
     response.name =   profile.getName(); 
     response.first_name = profile.getGivenName(); 
     response.last_name = profile.getFamilyName(); 
     response.image_url = profile.getImageUrl(); 
     response.email =  profile.getEmail(); 
     console.log(response); 

     //******* HOW TO GET THE "image.isDefault" RESULT? ****** 


     }, function(error) { 
      alert(JSON.stringify(error, undefined, 2)); 
     }); 
    } 
    </script> 
    </head> 
    <body> 
    <div id="gSignInWrapper"> 
    <span class="label">Sign in with:</span> 
    <div id="customBtn" class="customGPlusSignIn"> 
     <span class="icon"></span> 
     <span class="buttonText">Google</span> 
    </div> 
    </div> 
    <div id="name"></div> 
    <script>startApp();</script> 
</body> 
</html> 

這是我在尋找對象:

ageRange: Object 
    min: 21 
circledByCount: 0 
displayName: "John Smith" 
etag: ""FT7asdjf83294289sf/sh-UjzBIkCHasfdj283429sf"" 
id: "1127384729842983928" 
image: 
    isDefault: true 
    url: "https://lh3.googleusercontent.com/-XYuefSD/AAAAAAAAAAI/AAAAAAAAAAA/23visd238/photo.jpg?sz=50" 
isPlusUser: true 
kind: "plus#person" 
language: "en" 
name: 
    familyName: "Smith" 
    givenName: "John" 
objectType: "person" 
url: "https://plus.google.com/118948298492849237" 
verified: false 
+0

它只是重新打開默認圖像 – Viney

回答

0

這裏是我的回答:

var googleUser = {}; 

//This method sets up the sign-in listener after the client library loads. 
var startApp = function() { 
    gapi.load('auth2', function(){ 
     gapi.client.load('plus','v1').then(function() { 

      // Retrieve the singleton for the GoogleAuth library and set up the client. 
      auth2 = gapi.auth2.init({ 
       client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com', 
       cookiepolicy: 'single_host_origin' 
       //scope: 'additional_scope' 
      }); 

      attachSignin(document.getElementById('custom-google-btn')); 
     }); 

    }); 
}; 


function attachSignin(element) { 

    auth2.attachClickHandler(element, {}, 

     function(googleUser) { 

      var authResult = gapi.auth2.getAuthInstance(); 
      if (authResult.isSignedIn.get()) { 

       gapi.client.plus.people.get({ 
        'userId': 'me' 
       }).then(function(res) { 

        var profile = res.result; 
        console.log(profile); 
        //******All the image data is available here****** 
        console.log(profile.image.isDefault); 

       }); 

      } else { 

       //alert('not signed in'); 
       if (authResult['error'] || authResult.currentUser.get().getAuthResponse() == null) { 
        // There was an error, which means the user is not signed in. 
        // As an example, you can handle by writing to the console: 
        console.log('There was an error: ' + authResult['error']); 
       } 

      } 

     }, function(error) { 
      //alert(JSON.stringify(error, undefined, 2)); 
     }); 
}