【问题标题】:Angularjs + Google oauth2 + get id_tokenAngularjs + 谷歌 oauth2 + 获取 id_token
【发布时间】:2016-10-25 11:17:37
【问题描述】:

我正在尝试在 angualjs 中实现 google auth。 但我研究并获得了 javascript 代码。

代码:

<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(不是访问令牌)。

【问题讨论】:

    标签: javascript angularjs oauth-2.0 google-authentication


    【解决方案1】:

    我认为你应该试试angular-google-plus 模块。它是一个完整的 Angular 模块,可以使用 Google+ API 处理登录。

    这是DEMO(别忘了在代码中插入您的客户 ID。)

    使用示例:

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

    我认为authResult 对象应该包含您想要的内容。

    【讨论】:

    • 抱歉回复晚了。我希望 authResult 包括 id_token。但现在它没有得到 id_token
    猜你喜欢
    • 2016-10-26
    • 2014-11-21
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2016-03-12
    相关资源
    最近更新 更多