【问题标题】:how to create GCE instance via api如何通过 api 创建 GCE 实例
【发布时间】:2014-04-09 10:42:54
【问题描述】:

因变量是:

$randomString 是随机字符串

$num_rows[0] oAuth 2.0 处理后的正确 access_token


请求:

$url = "https://www.googleapis.com/compute/v1/projects/plenary-ability-439/zones/us-central1-a/disks?sourceImage=https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-6-v20140318&access_token=".$num_rows[0];

    $params = array(

                "kind" => "compute#disk",
                "zone" => "https://www.googleapis.com/compute/v1/projects/plenary-ability-439/zones/us-central1-a",
                "name" => $randomString,
                "description" => "any description "
            );
          $ch = curl_init();

            curl_setopt($ch, CURLOPT_HEADER,0);           

          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
          $headers = array("Content-Type: application/json" ); 
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          $result = curl_exec($ch);
          $res = json_decode($result, true);

回复:

{

“错误”:{

“错误”:[

{

"domain": "global",

"reason": "parseError",

"message": "This API does not support parsing form-encoded input."

}

],

“代码”:400,

"message": "此 API 不支持解析表单编码输入。"

}

}

$headers = array("Content-Type: application/json" ); 是我目前所知道的并且我已经正确设置了它

*第二件事是:我也试过这个,但我得到了同样的回应*

     $headers = array(
        "Content-Type: application/json",
        "Authorization: Bearer $num_rows[0]", 
    );

【问题讨论】:

    标签: php google-app-engine curl google-compute-engine


    【解决方案1】:

    您正在向 google 发送 http GET 请求。所以用这个:

    $sourceImage = urlencode('https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-6-v20140318');
    $access_token = urlencode($num_rows[0]);
    $url = "https://www.googleapis.com/compute/v1/projects/plenary-ability-439/zones/us-central1-a/disks?sourceImage=$sourceImage&access_token=$access_token";
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    curl_close($ch);
    $res = json_decode($result, true);
    

    【讨论】:

    • Hanssan :无论如何我在 python 中使用 Google 客户端库尝试过,它都不起作用
    【解决方案2】:

    access_token 不应是 URL 的一部分

    $url = "https://www.googleapis.com/compute/v1/projects/plenary-ability-439/zones/us-central1-a/disks?sourceImage=https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-6-v20140318

    访问令牌成为标头的一部分

    {'Authorization': 'OAuth ' + access_token, 
    'x-goog-api-version': version,
    'x-goog-project-id': project_id,
    'content-type': 'application/json'}
    

    另外你是如何获取 access_token 的,我猜你是使用元数据服务器来获取它的。

    参考https://developers.google.com/compute/docs/authentication#applications

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-19
      • 2016-07-11
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      相关资源
      最近更新 更多