0
votes

dependent variables are:

$randomString which is random string

$num_rows[0] which is correct access_token after oAuth 2.0 process


Requet :

$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);

Respnse :

{

"error": {

"errors": [

{

"domain": "global",

"reason": "parseError",

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

}

],

"code": 400,

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

}

}

$headers = array("Content-Type: application/json" ); is onething i come know so far and i have set this correctly

*second things is : i also tried this but i got same response *

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

2 Answers

0
votes

You are doing http GET request to the google. So use this one:

$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);
0
votes

The access_token should not be part of the 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

Access token becomes part of the header

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

Also how are you getting the access_token, I am guessing you are using metadata server to get it.

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