【问题标题】:Request had insufficient authentication scopes (Google Cloud Resource Manager)请求的身份验证范围不足(Google Cloud 资源管理器)
【发布时间】:2020-04-14 23:43:42
【问题描述】:

我正在尝试在 Angular 项目中调用 Google Cloud Resource Manager,并且我正在使用 angular/fire 来使用 Firebase 服务。所以我使用 angular/fire 的 auth 方法,并在弹出用户之间使用 google 的 oAuth2 进行身份验证,并返回凭据和令牌。

当我使用令牌调用 API 时,会向我发送一个错误:

"error":{"code":403,"message":"Request had insufficient authentication scopes.","status":"PERMISSION_DENIED"}}

我的电话在下面:

createProject(token): Observable<any>{


    const body = {
      "name": 'newAgent',
      "projectId": 'newAgent123',
      "labels": {
        "test": 'test'
      }
    }


    return this._http.post(`
      https://cloudresourcemanager.googleapis.com/v1/projects?
      access_token=${token}`,
      body
    )
  }

【问题讨论】:

    标签: angular api google-cloud-platform


    【解决方案1】:

    您必须确保您用于身份验证的服务帐户具有适当的访问范围

    它必须有 Datastore access scope (https://www.googleapis.com/auth/datastore) 才能与 Firestore 的 API 对话。 你可以在这里阅读更多内容how Firestore API works

    您可以在虚拟机详情页面set proper scopes for your service account using console

    更改范围后,需要重新启动实例 - 否则将不会应用。

    Stack Overflow here 上也讨论了类似的情况。这可能会有所帮助。

    【讨论】:

    • 如果我理解的话,它需要 IAM 权限凭证级别才能使用 ir,也许 firestore auth 范围不适用于 Cloud Resource Manager?
    【解决方案2】:

    我找到了一种解决方案来验证并获得正确的范围: 首先:创建一个 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 }
        )
      }
    

    【讨论】:

      猜你喜欢
      • 2016-06-21
      • 2021-11-22
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 2016-04-28
      • 2021-08-07
      • 2021-01-04
      • 1970-01-01
      相关资源
      最近更新 更多