I’m working on writing a simple database that uses Google Fusion Tables to store and retrieve data; however, I’ve hit a snag with the authorization process. Here’s how my code looks so far:
<?php
// Establish login data for Google account hosting Fusion Table.
$login_data = array(
'accountType' => 'GOOGLE',
'Email' => 'xxxxxxxxxxxxxxx@xxxxx.xxx',
'Passwd' => 'xxxxxxxxxxx',
'service' => 'fusiontables',
'source' => 'jordanMeyer-musicNotes-1'
);
$login_query = http_build_query($login_data);
// Establish the POST request and send it to Google's server.
$google = curl_init('https://www.google.com/accounts/ClientLogin');
curl_setopt($google, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($google, CURLOPT_POST, TRUE);
curl_setopt($google, CURLOPT_POSTFIELDS, $login_query);
$results = curl_exec($google);
$results = explode("\n", $results);
$token = $results[2];
$token = substr($token, 5);
$auth_header = array(
'Authorization' => 'GoogleLogin auth=' . $token
);
curl_close($google);
$insert_data = array(
'sql' => 'INSERT INTO 2317404 (Artist) VALUES (\'Test\')',
);
$insert_query = http_build_query($insert_data);
$insert = curl_init('https://www.google.com/fusiontables/api/query');
curl_setopt($insert, CURLOPT_POST, TRUE);
curl_setopt($insert, CURLOPT_HTTPHEADER, $auth_header);
curl_setopt($insert, CURLOPT_POSTFIELDS, $insert_query);
curl_exec($insert);
?>
When testing this code, I keep getting a 401 error saying that “Unauthenticated access is allowed only for SELECT andDESCRIBE statements.” I’m unsure as to why this is happening when I am passing the authorization header too the server in the POST operation to the Fusion Tables service. Any ideas as to what I’m doing wrong?
I just looked into things further and found out that it was a coding mistake on my part. I’ll go ahead and answer it here just in case any other rookie web developers are having this kind of issue…
When sending the POST to the Fusion Tables server, I had made an array in which the soon-to-be HTTP header values were stored. I did it in a way like this:
I was expecting this to output a header of “Authorization: GoogleLogin auth=…”; however, I was mistaken in my understanding making HTTP headers in cURL. While it DOES require an array of values, the array doesn’t require index variables. That being said, the proper way to write this into the code would be as follows:
It turns out I was overcomplicating things for myself. Sorry for asking such a rookie question. Hopefully this answer will be useful to someone else in my situation!