Google OAuth2要求提供两次帐户详细信息

时间:2017-03-10 07:11:04

标签: javascript google-oauth2 google-oauth-java-client

我正在使用javascript通过Google使用OAuth对用户进行身份验证。 我让用户登录,但帐户选择器已打开两次,它会一次又一次地选择帐户,然后要求该帐户,然后每次都要求离线访问。

我需要选择一次帐户并选择离线访问,这里是代码。

GoogleAuth.signIn({
        scope: 'profile email'
    }).then(function (result: any) {

        //if successful, then only call below. else, return error.

        var user = GoogleAuth.currentUser.get();
        var email = user.getBasicProfile().getEmail();
        var userName = user.getBasicProfile().getName();
        GoogleAuth.grantOfflineAccess({prompt: "consent"}).then(function (authResult: any) {
            if (authResult['code']) {

                // Send the code to the server
                $.ajax({
                    type: 'POST', 
                    url: self._config.apiEndPoint +  'v1/authorization/ProcessAuthorizationCode?authorizationCode=' + authResult['code'] + "&userId=" + email,
                    // Always include an `X-Requested-With` header in every AJAX request,
                    // to protect against CSRF attacks.
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest'
                    },
                    contentType: 'application/octet-stream; charset=utf-8',
                    success: function (result) {
                        //call register external now

                        $.ajax({
                            type: 'POST',
                            url: self._config.apiEndPoint + 'v1/account/registerexternal?client_id=' + self._config.appClientId,
                            // Always include an `X-Requested-With` header in every AJAX request,
                            // to protect against CSRF attacks.
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest'
                            },
                            contentType: 'application/json',
                            success: function (result) {
                                self._profileData.UserEmail = email;
                                self._profileData.UserName = userName;
                                self._router.navigateByUrl("/profile");
                            },
                            processData: false,
                            data: JSON.stringify({
                                ExternalUserId: email,
                                UserName: userName,
                                Provider: 'Google',
                                ExternalAccessToken: result.access_token,
                                RefreshToken: 'Not really important',
                                Email: email,
                                ProviderKey: 'Not really important'
                            }), 
                            error: function (xhr) {

                                self._router.navigateByUrl("/error;type=" + JSON.parse(xhr.responseText).Message, {preserveQueryParams:true});
                            }
                        });
                    },
                    processData: false,
                    data: authResult['code']
                });
            } else {
                // There was an error.
            }
        });

     });

0 个答案:

没有答案