Google 身份验证/Gapi:未捕获类型错误:无法读取 null 的属性“postMessage”

2023-12-25

祝大家有美好的一天!

我正在使用创建一个网络应用程序Angular.js 1.5, 谷歌身份验证(gapi) and 用户界面路由器.

我的主要目标是初始化谷歌用户首先,使用解决方法从主要抽象状态:

$stateProvider
    .state('app', {
      url:'/',
      templateUrl: 'layout/app-view.html',
      resolve: {
        googleAuth: function (User:UserService) {
          return User.initCurrent();
        }
      }
    });

此时效果非常好 - 它注入用户服务,并称其为初始化当前()方法,返回一个promise一旦当前用户被初始化,它将被完整归档。这是UserService简洁代码:

class UserService implements UserInterface {
  public current:GoogleUser;
  private _GoogleAuth:GoogleAuthService;
  private _AppConstants;

  constructor(GoogleAuth:GoogleAuthService) {
    'ngInject';
    this._GoogleAuth = GoogleAuth;
  }

  initCurrent():ng.IPromise<GoogleUser> {
    return this._GoogleAuth.getCurrentUser().then((current) => {
      this.current = current;
      return this.current;
    })
  }
}

轮到时初始化当前()收到来自的承诺获取当前用户()一旦实现gapi将被加载并且谷歌验证实例将被初始化。为此我创建了一个谷歌验证服务:

class GoogleAuthService implements GoogleAuthServiceInterface {
  public googleAuth;
  private _AppConstants;
  private deferGoogleAuthInit:ng.IDeferred<any>;

  constructor(AppConstants, $log, $q:ng.IQService) {
    'ngInject';
    this._AppConstants = AppConstants;
    this.deferGoogleAuthInit = $q.defer();
    gapi.load('auth2', () => {
      gapi.auth2.init({
        client_id: AppConstants.googleClientId,
        cookiepolicy: 'single_host_origin'
      }).then((googleAuth) => {
        this.googleAuth = googleAuth;
        this.deferGoogleAuthInit.resolve('GoogleAuth Initialized');
      })
    });
  }
  getCurrentUser() {
    return this.deferGoogleAuthInit
      .promise.then(() => {
        return this.googleAuth.currentUser.get()
      })
  }
}

问题:当我不使用解析方法来初始化用户时,并通过控制台执行相同的操作:获取 UserService 实例、initCurrentUser 等。一切正常:登录/退出、获取当前用户等。 但是,当我在解析方法中初始化当前用户时,尝试注销会导致以下错误消息:

未捕获的类型错误:无法读取 null 的属性“postMessage”

但与此同时,所有服务都正确注入,并且我收到了正确的用户实例,尽管我无法重新登录它。你有什么建议?


我在反应应用程序上遇到了类似的问题。我能够通过更改 CORS 策略来解决,如下所示:

Cross-Origin-Opener-Policy: same-origin-allow-popups

救世主参考-

https://github.com/google/google-api-javascript-client/issues/796#issuecomment-1118136612 https://github.com/google/google-api-javascript-client/issues/796#issuecomment-1118136612

(发现自 -为什么 Django 中的 Google-Auth(Google Identity) 弹出窗口空白? https://stackoverflow.com/questions/71371354/why-google-authgoogle-identity-blank-popup-in-django)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google 身份验证/Gapi:未捕获类型错误:无法读取 null 的属性“postMessage” 的相关文章

随机推荐