AngularFire 身份验证访问 Google AccessToken

AngularFire Authentication Access to Google AccessToken

使用Firebase Web SDK,您可以按如下方式引用AccessToken:

const provider = new auth.GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/plus.login');

firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Google Access Token. You can use it to access the Google API.
  var token = result.credential.accessToken;
  // The signed-in user info.
  var user = result.user;
  // ...
});

您随后可以使用 token 获取有关您的用户的更多信息;例如:

public getFirstName(token: string): Observable<string> {
  const url = 'https://www.googleapis.com/oauth2/v3/userinfo?access_token=' + token;

  return this.http.get(url).map(response => {
    return response.json().given_name;
  });
}

问题:如何使用 AngularFire2 protocol 检索此访问令牌?

public loginGoogle() {
  this.af.auth.login().then((authState) => {
    authState.WHERE_IS_IT; // <<<----- 
  })
}

提前致谢! // 凯文

implementation of login calls the authentication backend (the SDK) and passes the received UserCredential to authDataToAuthState - credential 作为 providerData 传递。

authDataToAuthState 有一个 switch 语句,将 credential 映射到提供程序相关的 属性。对于 google.com,凭据应存储在 google 属性:

case 'google.com':
  authState.google = providerData; // i.e. the credential
  authState.provider = AuthProviders.Google;
  break;

请注意,将从候选版本的 AngularFire2 中删除身份验证方法。有关于 here 的讨论。删除后,SDK 将直接用于登录等 - 希望这会减少混乱。