Аутентифицировать запросы после входа в систему с помощью GoogleSignInComponent в Angular Webapplication - PullRequest
0 голосов
/ 09 сентября 2018

Я пишу Angular-Web-приложение, в котором хочу перечислить проекты Google Cloud Platform зарегистрированного пользователя.

Для входа в систему я использовал пакет 'angular-google-signin' npm из npm с областью действия * https://www.googleapis.com/auth/cloud-platform.read-only', как упомянуто в API Explorer от Google.

Возвращает событие, содержащее gapi.auth2.GoogleUser с AuthResponse, которое выглядит следующим образом:

{
  "token_type": "Bearer",
  "access_token": "some token",
  "scope": "https://www.googleapis.com/auth/cloud-platform.read-only https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me openid email profile",
  "login_hint": "some hint",
  "expires_in": 2077,
  "id_token": "some id Token",
  "session_state": {
    "extraQueryParams": {
      "authuser": "0"
    }
  },
  "first_issued_at": 1536444745852,
  "expires_at": 1536448345852,
  "idpId": "google"
}

Но мне не удается создать запрос на Google API. Gapi.client всегда неопределен, а также простой REST-запрос с access_token в заголовке, так как Bearer: access_token не работает. Что мне не хватает?

Обратный вызов компонента angular-google-signin выглядит так, и пользователь успешно вошел в систему:

  onGoogleSignInSuccess(event: GoogleSignInSuccess) {
    // console.log(JSON.stringify(event));
    const googleUser: gapi.auth2.GoogleUser = event.googleUser;
    console.log(JSON.stringify(googleUser.getAuthResponse()));
    const id: string = googleUser.getId();
    const profile: gapi.auth2.BasicProfile = googleUser.getBasicProfile();
    console.log('ID: ' +
      profile
        .getId());
    console.log('Name: ' + profile.getName());

    // request({
    //   uri: 'https://cloudresourcemanager.googleapis.com/v1/projects',
    //   method: 'GET',
    //   headers: {
    //     'Bearer': googleUser.getAuthResponse().access_token
    //   }
    // }, res => {
    //   console.log(res);
    // });

    // gapi.client.load('cloudresourcemanager', 'v1').then((value: any) => {
    //   console.log(value);
    // }).catch((error: any) => {
    //   console.error(error);
    // });
  }

Как мне создать и аутентифицировать запрос на выборку пользовательских проектов? Я не могу заставить его работать.

Заранее спасибо

1 Ответ

0 голосов
/ 09 сентября 2018

Хорошо, теперь я чувствую себя немного глупо:

добавление? Access_token = xxxxx к URL решило это.

...