Google使用PHP跟踪API“错误请求”

I am using Google Tracks API to build a simple web based program to track a vehicle that has a tracking device sending latitude and longitude coordinates. I am using PHP and the OAuth2 PHP library to make an authorized connection.

After authorizing and getting an access token I am making a request to create entities. Though I can't seem to get this working and keep getting a "400 Bad Request" response. Following all the steps shown in the documentation.

Here is my code:

$url = 'https://www.googleapis.com/tracks/v1/entities/create/?access_token='.$parsedAuth['access_token'];

$data = array('entities' => array( "name"=> "Chevrolet" ));
$json_data = json_encode($data);
$data_length = http_build_query($data);

$options = array(
     'http' => array(
            'header'  => "Content-type: application/json
". "Content-Length: " . strlen($data_length) . "
",
            'method'  => 'POST',
            'content' => $json_data
        ),
    );

    $context  = stream_context_create($options);
    $response = file_get_contents($url, false, $context);

    var_dump($response);

Exact Error is: "failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request"

Why am I getting a bad request? What would be a good request that will register these entities and return id's? Thank you

The answer given here is wrong. The documentation states that it must be a POST see here My issue was not with the Auth but with the Tracks API itself. I ended up moving to create the request with CURL and it works just fine.

Please. This is PHP with CURL. It works 100%.

//Google maps tracks connection
//Get Files From PHP Library
require_once 'google-api-php-client/src/Google/autoload.php';
require_once 'google-api-php-client/src/Google/Service/MapsEngine.php';

//Set Client Credentials
$client_id = '*************.apps.googleusercontent.com'; //Client ID
$service_account_name = '************@developer.gserviceaccount.com'; //Email Address
$client_email = '*************@developer.gserviceaccount.com';
$private_key = file_get_contents('************.p12');
$scopes = array('https://www.googleapis.com/auth/tracks');

//Create Client
$client = new Google_Client();

$client->setApplicationName("Client_Library_Examples");

//Send Credentials
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);
$client->setAssertionCredentials($credentials);

if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($credentials);
}

if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}

$client->setAssertionCredentials($credentials);

$_SESSION['service_token'] = $client->getAccessToken();
foreach ($_SESSION as $key=> $value) {
    $vars = json_decode($value);
}
$parsedAuth = (array) $vars;
$token = $parsedAuth['access_token'];
//all functions in the program use this auth token- It should be global for easy accesses. 
global $token;


function createEntities(){
    global $token;
    $url = 'https://www.googleapis.com/tracks/v1/entities/create/?access_token='.$token;

    //FIX ME: fields is temporarily hard coded- should be brought from DB
    $fields = array(
                'entities' => array(
                    'name' => "DemoTruck",
                    'type' => "AUTOMOBILE"
                    ),
        );

    //json string the data for the POST
    $query_string = '';
    foreach($fields as $key => $array) {
        $query_string .= '{"' . urlencode($key).'":[{';
        foreach($array as $k => $v) {
            $query_string .= '"' . urlencode($k) . '":"' . urlencode($v) . '",';
        }
    }
    $str = rtrim($query_string , ',');
    $fstr = $str.'}]}';

    $length = strlen( $fstr );
    //open connection
    $ch = curl_init();
    //test connection
    if (FALSE === $ch)
    throw new Exception('failed to initialize');

    //set options
    $header = array('Content-type: application/json');
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fstr);

    $result = curl_exec($ch);

    //dump in case of error
    if (FALSE === $result){
        var_dump( curl_error($ch) );
        var_dump( curl_getinfo($ch) ); 
    } 

    //close connection
    curl_close($ch);
}