2016-06-23 8 views
4

私はangualjsでgoogle authを実装しようとしています。 しかし、私は調査し、javascriptでコードを取得します。Angularjs + Google oauth2 + get id_token

コード:

<html lang="en"> 
    <head> 
     <meta name="google-signin-scope" content="profile email"> 
     <meta name="google-signin-client_id" content="148699057185-ug1ge86g4dn4uffffekth3rb7382cl2333323238fau.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); 

       alert(id_token); 

       // All HTML5 Rocks properties support CORS. 

      } 
     </script> 
    </body> 
</html> 

私はid_token警告取得しています。

しかし、私はangularjsに変換する必要があります。

私はangularjsに実装しようとしましたが、動作しませんでした。

私を助けてください。

予想:私はangularjsのgoogle oauthからid_token(アクセストークンではない)のみを取得する必要があります。

答えて

1

angular-google-plusモジュールを試してください。これは、Google+ APIを使用してログインを処理する完全な角度のモジュールです。私はauthResultオブジェクトが何をしたい含まれているべきだと思う

var app = angular.module('app', ['googleplus']); 

app.config(['GooglePlusProvider', function(GooglePlusProvider) { 
    GooglePlusProvider.init({ 
     clientId: 'YOUR_CLIENT_ID', 
     apiKey: 'YOUR_API_KEY' 
    }); 
}]); 

app.controller('AuthCtrl', ['$scope', 'GooglePlus', function ($scope, GooglePlus) { 
    $scope.login = function() { 
     GooglePlus.login().then(function (authResult) { 
      console.log(authResult); 

      GooglePlus.getUser().then(function (user) { 
       console.log(user); 
      }); 
     }, function (err) { 
      console.log(err); 
     }); 
    }; 
}]); 

:ここ

DEMO(コード内でクライアントIDを挿入することを忘れないでください)

使用例です。

+0

ご返信が遅くなりました。私はid_tokenを含むauthResultを期待しています。今はid_tokenを取得していない – RSKMR

関連する問題