【问题标题】:WHAT HEADERS MUST I USE TO USE GOOGLE API我必须使用什么标题才能使用 GOOGLE API
【发布时间】: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


【解决方案1】:

已经有javascript library 可以轻松地为您处理这一切。它还支持不同的身份验证方式:OAuth2、服务到服务和 API 密钥。对于 oauth2 ,你的情况可能是这样的:

const oauth2Client = new google.auth.OAuth2(
  YOUR_CLIENT_ID,
  YOUR_CLIENT_SECRET,
  YOUR_REDIRECT_URL
);
// a get request to this url that will send you back the code
const url = oauth2Client.generateAuthUrl({
  // 'online' (default) or 'offline' (gets refresh_token)
  access_type: 'offline',

  // If you only need one scope you can pass it as a string
  scope: [] // the needed list of scopes
});

// use the code to get the tokens
const {tokens} = await oauth2Client.getToken(code)
oauth2Client.setCredentials(tokens);

现在对于资源管理器 api,你可以检查这个example here

【讨论】:

  • 谢谢,但它对我不起作用,因为问题出在资源管理器 API 中,不要向我发送响应,我研究发现问题可能出在 CORS 中。但我感谢你向我展示了一种让以前的工作更干净的方法
猜你喜欢
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多