【发布时间】:2020-04-16 03:08:37
【问题描述】:
我认为我需要更多标头才能使用 Google Cloud Resource Manager API。 这是我进行身份验证和获得许可的过程。但现在 API 不会向我发送解释错误。
首先:创建一个 oAuth 弹出窗口:
openPopup()
{
const name = 'Authorization'
const options = `width=${ 500 },height=${ 600 },left=${ 0 },top=${ 0 }`;
const url = `
https://accounts.google.com/o/oauth2/v2/auth?
client_id=<CLIENT_ID>&
response_type=code&
include_granted_scopes=true&
scope=https%3A//www.googleapis.com/auth/cloud-platform&
redirect_uri=http://localhost&
access_type=offline`;
return window.open(url, name, options);
}
然后验证代码:
authApi(): Observable<any>{
let headers = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded'
})
let body = `
code=<RESPONSE_CODE>&
client_id=<CLIENT_ID>&
client_secret=<CLIENT_SECRET>&
grant_type=authorization_code&
redirect_uri=http://localhost`;
return this._http.post(`https://www.googleapis.com/oauth2/v4/token`,
body, {headers: headers})
}
但是现在,当我尝试使用 API 时会向我发送此错误
status: 0
statusText: "Unknown Error"
url: "https://cloudresourcemanager.googleapis.com/v1/projects"
ok: false
name: "HttpErrorResponse"
message: "Http failure response for https://cloudresourcemanager.googleapis.com/v1/projects: 0 Unknown Error"
我尝试将 API 与下一个函数一起使用
createProject( token ): Observable<any>{
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
'scope': 'https://www.googleapis.com/auth/cloud-platform',
'Authorization': `Bearer ${token}`
})
const body = {
"name": 'newAgent',
"projectId": 'newAgent123',
"labels": {
"test": 'test'
}
}
return this._http.post(`https://cloudresourcemanager.googleapis.com/v1/projects`,
body, { headers: headers }
)
}
【问题讨论】:
-
再次检查此行的返回值:
return this._http.post('https://www.googleapis.com/oauth2/v4/token'。返回的数据是一个 JSON 结构。解析后要返回键token的值。 -
当然,我不会假装返回 json 响应。就在我使用 createProject 函数(最后一个)时,我发送 access_token 属性
标签: angular google-app-engine google-cloud-platform oauth google-api