Я пишу Angular-Webapplication, в котором я хочу перечислить проекты Google Cloud Platform для зарегистрированного пользователя.
Чтобы войти в систему, я использовал пакет npm 'angular-google-signin' из 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"
}
Но мне не удается создать запрос к API Google. Gapi.client всегда не определен, а также простой REST-запрос с access_token в заголовке как Bearer: access_token не работает. Что мне не хватает?
Обратный вызов компонента angular-google-signin выглядит так, и пользователь успешно вошел в систему:
onGoogleSignInSuccess(event: GoogleSignInSuccess) {
// console.info(JSON.stringify(event));
const googleUser: gapi.auth2.GoogleUser = event.googleUser;
console.info(JSON.stringify(googleUser.getAuthResponse()));
const id: string = googleUser.getId();
const profile: gapi.auth2.BasicProfile = googleUser.getBasicProfile();
console.info('ID: ' +
profile
.getId());
console.info('Name: ' + profile.getName());
// request({
// uri: 'https://cloudresourcemanager.googleapis.com/v1/projects',
// method: 'GET',
// headers: {
// 'Bearer': googleUser.getAuthResponse().access_token
// }
// }, res => {
// console.info(res);
// });
// gapi.client.load('cloudresourcemanager', 'v1').then((value: any) => {
// console.info(value);
// }).catch((error: any) => {
// console.error(error);
// });
}
Как создать и подтвердить запрос на получение проектов пользователей? Я не могу заставить его работать.
заранее спасибо
Хорошо, я чувствую себя немного глупо:
добавление? access_token = xxxxx к URL-адресу решило эту проблему.
Думаю, вы просто неправильно указали заголовок:
request({
uri: 'https://cloudresourcemanager.googleapis.com/v1/projects',
method: 'GET',
headers: {
'Authorization': 'Bearer ' + googleUser.getAuthResponse().access_token
}
}, res => {
console.info(res);
});