2015-09-10 18 views
7

Chúng tôi có một ứng dụng dựa trên Google để xác thực người dùng của mình đối với tài khoản ứng dụng google của chúng tôi và sau đó thực hiện một số xác minh và tra cứu nhóm trên máy chủ.API Google+ không trả về access_token Javascript

Gần đây, google đã thay đổi tên của đối tượng đã giữ biến access_token mà chúng tôi yêu cầu để xác thực. Trong các tài liệu (https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile) nó nói rằng access_token có sẵn từ phương thức getAuthResponse(), tuy nhiên khi tôi sử dụng nó trở lại là không xác định. Kiểm tra đối tượng sau console.log() cho thấy tất cả các trường khác được đề cập ngoại trừ access_token. Tôi lo lắng rằng Google sẽ thay đổi đối tượng một lần nữa trong tương lai và để lại cho chúng tôi mà không có ứng dụng của chúng tôi. Đây là mã.

<head> 
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css"> 
<script src="https://apis.google.com/js/platform.js" async defer></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
<meta name="google-signin-client_id" content="XXX.apps.googleusercontent.com"> 
<script> 
    //This happens after the user has authenticated with Google and has been passed 
    //back to the page 
     function onSignIn(googleUser) { 
      //Check to see whether the user is trying to sign out. 
      if (window.location.href.indexOf("signOut=1") !== -1) { 
       //Sign them out of the application. 
       signOut(); 
       //redirect them to the same page, without the signOut query string so they can log back in if want 
       window.location.href='googlesigninform.html' 
       return false; 
      } 
      //Grab the token, access token and email. 
      var _id = googleUser.getAuthResponse().id_token; //This works 
      var _accessToken = googleUser.Ka.access_token; //This works but changed from googleUser.B.access_token 
      var profile = googleUser.getBasicProfile(); //Works 
      console.log(googleUser.access_token); //Undefined 
      console.log(googleUser.getAuthResponse().access_token);//Undefined 
      //Make a post request to the API 
      makePostRequest(_id, _accessToken, profile.getEmail()); 
     } 

Cách chính xác để truy cập biến access_token là gì?

+0

Đây có phải là ngu ngốc của một câu hỏi mà không ai trả lời? –

+0

Xảy ra một lần nữa đêm qua. Các đối tượng thô một lần nữa thay đổi tên và phá vỡ các ứng dụng của chúng tôi. –

Trả lời

1

Tôi cũng có một công việc hack xung quanh nhận access_token từ biến.

function findAccessToken(googleUser) { 
      var returnValue; 
      Object.getOwnPropertyNames(googleUser).forEach(function (val, idx, array) { 
       console.log(val + ' -> ' + googleUser[val]); 
       Object.getOwnPropertyNames(googleUser[val]).forEach(function (vals, idxs, arrays) { 
        if (vals === "access_token") { 
         console.log("true"); 
         returnValue = googleUser[val][vals]; 
        } 
       }); 

      }); 
      return returnValue; 
     } 

Chắc chắn đây không phải là giải pháp thanh lịch nhất. Nếu ai đó có thể chỉ vào hướng sáng hơn sẽ tốt hơn.

4

Nếu bạn cần sử dụng mã thông báo truy cập, bạn đang sử dụng sai loại luồng đăng nhập google. Bạn nên làm theo trang này: https://developers.google.com/identity/sign-in/web/server-side-flow

Những gì bạn đã thực hiện là google Sign-In để xác định người dùng (https://developers.google.com/identity/sign-in/web/)

Mà chỉ cung cấp một id duy nhất cho mỗi người dùng bởi vì nó có nghĩa là để xác thực người dùng cho riêng bạn dịch vụ và không cung cấp mã thông báo truy cập để sử dụng cho các dịch vụ khác của Google sau này.

4

Tôi tin rằng vấn đề của bạn là ứng dụng của bạn thiếu google-signin-scope cần thiết.

Để trả lời câu hỏi của bạn, tôi đã tạo ứng dụng từ mặt đất bằng cách sử dụng Google Developer Console. Ứng dụng rất đơn giản giống như ứng dụng này tutorial.

Toàn bộ ứng dụng bao gồm HTML đơn giản tải API Google và có gọi lại được gọi là onSignIn (giống như của bạn).

Dưới đây là đoạn code entide của ứng dụng:

<html lang="en"> 

<head> 
    <meta name="google-signin-scope" content="profile email"> 
    <meta name="google-signin-client_id" content="PLACE_YOUR_ID_HERE.apps.googleusercontent.com"> 
    <script src="https://apis.google.com/js/platform.js" async defer></script> 
</head> 

<body> 
    <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div> 
    <script> 
    function onSignIn(googleUser) { 

     var response = googleUser.getAuthResponse(), 
     idToken = response['id_token'], 
     accessToken = response['access_token']; 

     console.dir('id token: ' + idToken); 
     console.dir('access token: ' + accessToken); 
    } 
    </script> 
</body> 

</html> 

Như bạn thấy, sự khác biệt giữa các ứng dụng của tôi và của bạn là bạn đang thiếu các META thuộc tính đầu tiên.

+0

Điều này có hiệu quả. Tôi chỉ lo ngại rằng điều này không nhất thiết phải đúng theo câu trả lời của Bas ở trên. Tôi sẽ xem xét nó ngày hôm nay và tìm ra. Cảm ơn –

+0

Tài liệu api https://developers.google.com/identity/sign-in/web/reference#googleusergetauthresponse chỉ định rằng nó trả về access_token.Câu trả lời của @Romulo giải thích tại sao kết quả của access_token là không xác định. Tôi đã cố gắng tái tạo vấn đề khi tôi cố tình bỏ qua phạm vi. Tôi nghĩ rằng đây sẽ là câu trả lời được chấp nhận. – user3240644

0

Đây là mã để đăng nhập bằng google.

<html lang="en"> 
    <head> 
    <meta name="google-signin-scope" content="profile email"> 
    <meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com"> 
    <script src="https://apis.google.com/js/platform.js" async defer></script> 
    </head> 
    <body> 
    <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div> 
    <script> 
     function onSignIn(googleUser) { 
     // Useful data for your client-side scripts: 
     var profile = googleUser.getBasicProfile(); 
     console.log("ID: " + profile.getId()); // Don't send this directly to your server! 
     console.log("Name: " + profile.getName()); 
     console.log("Image URL: " + profile.getImageUrl()); 
     console.log("Email: " + profile.getEmail()); 

     // The ID token you need to pass to your backend: 
     var id_token = googleUser.getAuthResponse().id_token; 
     console.log("ID Token: " + id_token); 
     }; 
    </script> 
    </body> 
</html> 
0

Hãy thử var này _access_token = GoogleUser.getAuthResponse(). Access_token

Các vấn đề liên quan