【问题标题】:AngularFire Authentication Access to Google AccessTokenAngularFire 身份验证访问 Google AccessToken
【发布时间】:2017-03-26 22:46:48
【问题描述】:

使用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; // <<<----- 
  })
}

提前致谢! // 凯文

【问题讨论】:

    标签: angular firebase-authentication angularfire2


    【解决方案1】:

    implementation of login 调用身份验证后端(SDK)并将接收到的UserCredential 传递给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 将直接用于登录等 - 希望这会让事情变得不那么混乱。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 2021-08-24
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      相关资源
      最近更新 更多