Fusion Tables - Service Account - Insert 403 Forbidden

1,765 views
Skip to first unread message

bobvmoran

unread,
Oct 4, 2012, 12:16:26 PM10/4/12
to google-api...@googlegroups.com
Hi,

I have been struggling with getting a service account up and running using the google-api-php-client library.
Its failing when I try to modify the data (update or insert).
It works when I run simple queries.
See below code for examples of both queries.
When I try to insert data I get the following error:
I really don't know where I'm going wrong, Im pretty sure everything is setup correctly on the api console, table is public.
Advice greatly appreciated!!!



"Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/fusiontables/v1/query?sql=INSERT%20INTO%201P8kJGgBvCYHOHWbPJwf8b8TtykociMAyzWq8Pz4%20%28stat_id%2C%20latitude%2C%20longitude%2C%20stat_type%29%20VALUES%20%28%272532%27%2C%2055.664503%2C%2012.59953%2C%20%27c%27%29: (403) Forbidden' in C:\xampp\htdocs\auto_obs\google-api-php-client\io\Google_REST.php:66 Stack trace: #0 C:\xampp\htdocs\auto_obs\google-api-php-client\io\Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 C:\xampp\htdocs\auto_obs\google-api-php-client\service\Google_ServiceResource.php(177): Google_REST::execute(Object(Google_HttpRequest)) #2 C:\xampp\htdocs\auto_obs\google-api-php-client\contrib\Google_FusiontablesService.php(182): Google_ServiceResource->__call('sql', Array) #3 C:\xampp\htdocs\auto_obs\cache_obs_data.php(50): Google_QueryServiceResource->sql('INSERT INTO 1P8...') #4 {main} thrown in C:\xampp\htdocs\auto_obs\google-api-php-client\io\Google_REST.php on line 66"





<--------------------------------------------------------------------------------------------------------------------------->

        // Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
const CLIENT_ID              = 'myclientid.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'myservicea...@developer.gserviceaccount.com';

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = 'mykeypath';

$client = new Google_Client();
$client->setApplicationName("my app name");
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
   SERVICE_ACCOUNT_NAME,
   $key)
);
$client->setClientId(CLIENT_ID);
$service = new Google_FusiontablesService($client);
$insQuery = "INSERT INTO 1P8kJGgBvCYHOHWbPJwf8b8TtykociMAyzWq8Pz4 (stat_id, latitude, longitude, stat_type) VALUES ('2532', 55.664503, 12.59953, 'c')";
// $selQuery = "select * from 1P8kJGgBvCYHOHWbPJwf8b8TtykociMAyzWq8Pz4 where rowid = '1'";
$res = $service->query->sql($insQuery);

<--------------------------------------------------------------------------------------------------------------------------->


I tried also calling:
$client::$auth->refreshTokenWithAssertion();
after 
$client->setAssertionCredentials(new Google_AssertionCredentials(...);
No affect ;(


KR,
Bob.

bobvmoran

unread,
Oct 5, 2012, 10:14:24 AM10/5/12
to google-api...@googlegroups.com
Ok so I finally got this to work and I thought Id share this with everyone In case you get stuck like I did.
One step that seems to be missing from all usage examples I've seen is the service account email address needs to be added via Google Docs.

  1. Select the document (FT) in Google Docs
  2. Click "Share"  
  3. Select "Add People"
  4. Enter your service account email address, e.g.     myservice...@developer.gserviceaccount.com
  5. Ensure "Can Edit" is selected.
  6. Un-check "notify people via email"
  7. Click "Share and Save"

This was a complete guess on my part but seems to have fixed my problem. 
I can finally insert into my FT via my service account.
Hope this helps.

Bob.


On Thursday, October 4, 2012 5:16:27 PM UTC+1, bobvmoran wrote:
Hi,

I have been struggling with getting a service account up and running using the google-api-php-client library.
Its failing when I try to modify the data (update or insert).
It works when I run simple queries.
See below code for examples of both queries.
When I try to insert data I get the following error:
I really don't know where I'm going wrong, Im pretty sure everything is setup correctly on the api console, table is public.
Advice greatly appreciated!!!



"Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/fusiontables/v1/query?sql=INSERT%20INTO%201P8kJGgBvCYHOHWbPJwf8b8TtykociMAyzWq8Pz4%20%28stat_id%2C%20latitude%2C%20longitude%2C%20stat_type%29%20VALUES%20%28%272532%27%2C%2055.664503%2C%2012.59953%2C%20%27c%27%29: (403) Forbidden' in C:\xampp\htdocs\auto_obs\google-api-php-client\io\Google_REST.php:66 Stack trace: #0 C:\xampp\htdocs\auto_obs\google-api-php-client\io\Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 C:\xampp\htdocs\auto_obs\google-api-php-client\service\Google_ServiceResource.php(177): Google_REST::execute(Object(Google_HttpRequest)) #2 C:\xampp\htdocs\auto_obs\google-api-php-client\contrib\Google_FusiontablesService.php(182): Google_ServiceResource->__call('sql', Array) #3 C:\xampp\htdocs\auto_obs\cache_obs_data.php(50): Google_QueryServiceResource->sql('INSERT INTO 1P8...') #4 {main} thrown in C:\xampp\htdocs\auto_obs\google-api-php-client\io\Google_REST.php on line 66"





<--------------------------------------------------------------------------------------------------------------------------->

        // Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
const CLIENT_ID              = 'myclientid.apps.googleusercontent.com';

Robin Michael

unread,
Feb 7, 2013, 9:25:50 AM2/7/13
to google-api...@googlegroups.com
Thanks, good finding. It helped me.
const SERVICE_ACCOUNT_NAME = 'myservicea...@developer.gserviceaccount.com';

Timothy Bloch

unread,
Sep 12, 2013, 3:14:21 PM9/12/13
to google-api...@googlegroups.com
Thank you!!! I never would have figured this out without your solution!
const SERVICE_ACCOUNT_NAME = 'myservicea...@developer.gserviceaccount.com';

Logan Meyer

unread,
Dec 7, 2013, 11:28:51 PM12/7/13
to google-api...@googlegroups.com
Thank you! This was a huge help!
const SERVICE_ACCOUNT_NAME = 'myservicea...@developer.gserviceaccount.com';
Reply all
Reply to author
Forward
0 new messages