Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google compute engine Google计算引擎:无法从gcloud终端查询API_Google Compute Engine_Google Cloud Platform - Fatal编程技术网

Google compute engine Google计算引擎:无法从gcloud终端查询API

Google compute engine Google计算引擎:无法从gcloud终端查询API,google-compute-engine,google-cloud-platform,Google Compute Engine,Google Cloud Platform,我在我的项目中创建了许多vm,我想从中查询gcloud终端的api。 我在尝试查询任何API时出现401-“需要登录”错误,即使我已被授权/登录到gcloud终端 C:\..\Google\Cloud SDK>gcloud config list [core] account = remis.haroon@*****.com disable_usage_reporting = True project = <proj-id> [meta] active_config = def

我在我的项目中创建了许多vm,我想从中查询gcloud终端的api。 我在尝试查询任何API时出现401-“需要登录”错误,即使我已被授权/登录到gcloud终端

C:\..\Google\Cloud SDK>gcloud config list
[core]
account = remis.haroon@*****.com
disable_usage_reporting = True
project = <proj-id>
[meta]
active_config = default

C:\..\Google\Cloud SDK>curl https://www.googleapis.com/compute/v1/projects/<proj-id>/aggregated/disks
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}
C:\..\Google\Cloud SDK>gcloud配置列表
[核心]
account=remis.haroon@*****.com
禁用使用情况报告=真
项目=
[元]
活动配置=默认值
C:\..\Google\CloudSDK>curlhttps://www.googleapis.com/compute/v1/projects//aggregated/disks
{
“错误”:{
“错误”:[
{
“域”:“全局”,
“原因”:“必需”,
“消息”:“需要登录”,
“位置类型”:“标题”,
“位置”:“授权”
}
],
“代码”:401,
“消息”:“需要登录”
}
}
如错误所述:

"message": "Login Required",
"locationType": "header",
"location": "Authorization"
由于您正在对GoogleAPI进行HTTP API调用,因此需要将OAuth访问令牌放入

GET compute/v1/projects//aggregated/disks HTTP/1.1
主持人:www.googleapis.com
授权:持票人XXXXXXXXXXXXXXXXX
有几种方法可以获得OAuth访问令牌。一个简单的方法是通过谷歌的

  • 选择所需的API作用域
  • 点击授权API按钮
  • 点击“交换代币授权码”
  • 您将在curl请求头中使用您的访问令牌

  • 使用REST API时需要携带授权令牌:

    gcloud身份验证登录

    TOKEN=$(gcloud auth打印访问令牌)

    curl-H“授权:持票人$TOKEN”

    GET compute/v1/projects/<proj-id>/aggregated/disks HTTP/1.1
    Host: www.googleapis.com
    Authorization: Bearer XXXXXXXXXXXXXXXXXXX