2015-09-10 5 views
7

私たちは、GoogleがGoogle Appsアカウントに対してユーザーを認証してから、サーバーサイド検証とグループ検索を行うアプリケーションを提供しています。Google+ APIはアクセストークンを返しませんJavascript

Googleは認証に必要なaccess_token変数を保持するオブジェクトの名前を変更しました。ドキュメント(https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile)では、access_tokenはgetAuthResponse()メソッドから取得できますが、これを使用するとundefinedとして返されます。 console.log()の後にオブジェクトを調べると、access_token以外のすべてのフィールドが表示されます。私は、Googleが今後オブジェクトを再び変更し、私たちのアプリケーションなしで私たちを残してしまうのではないかと心配しています。 ここにコードがあります。

<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()); 
     } 

access_token変数にアクセスする正しい方法は何ですか?

+0

をaccess_tokenは、これは誰が返信しないことに疑問の愚かですか? –

+0

昨夜もまたやりました。元のオブジェクトは再び名前を変更し、アプリケーションを壊しました。 –

答えて

1

まあ私は変数からaccess_tokenを取得するハックの仕事を持っています。

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; 
     } 

確かに、これは最も洗練された解決策ではありません。誰かがより良い方向に向けることができれば、それは良いことになるでしょう。

4

アクセストークンを使用する必要がある場合は、間違った種類のGoogleログインが使用されています。あなたが実装しなかった何https://developers.google.com/identity/sign-in/web/server-side-flow

Googleアカウントサインインがあなた自身のためにユーザを認証するためのものですので、唯一のユーザーごとに一意のIDを提供(https://developers.google.com/identity/sign-in/web/

をユーザを識別することです: このページに従ってください後で他のGoogleサービスに使用するアクセストークンを与えないようにしてください。

4

あなたの問題は、アプリケーションに必要なものがないと考えていますgoogle-signin-scope

あなたの質問に答えて、私はGoogle Developer Consoleを使って地面からアプリを作成しました。アプリケーションはこの1つのように非常に簡単ですtutorial

アプリケーション全体では、GoogleのAPIをロードし、(あなたのような)onSignInと呼ばれるコールバックを持って、単純なHTMLで構成されています。ここで

は、アプリケーションのentideコードです:

<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> 

あなたが見ることができるように、私のアプリとあなたの違いは、あなたが最初のMETA属性を欠いていることです。

+0

これはうまくいきます。私はちょうどこれが必ずしも上記のBasの答えに従って正しいとは限りません。私は今日それを見て、それを理解するでしょう。ありがとう –

+0

APIドキュメントhttps://developers.google.com/identity/sign-in/web/reference#googleusergetauthresponseは、access_tokenを返すように指定しています。@Romuloの答えは、access_tokenの結果がなぜ定義されていないのかを説明しています。私は意図的にスコープを抜けたときに問題を再現することができました。私はこれが受け入れられる答えだと思う。 – user3240644

0

ここに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

この VARを_access_token = GoogleUser.getAuthResponse()試してみてください。

関連する問題