我正在编写一个Angular-Web应用程序,我想在其中列出已登录用户的Google Cloud Platform项目。
为了登录用户,我使用了来自npm的'angular-google-signin'npm软件包,其范围为'https://www.googleapis.com/auth/cloud-platform.read-only',如Google的API Explorer所述。
这将返回一个包含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-Request,标头中的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);
// });
}
如何创建和验证获取用户项目的请求?我无法让它发挥作用。
提前致谢
好吧,我觉得现在有点愚蠢:
将?access_token = xxxxx添加到url解决了它。