Rich Standbrook
Rich Standbrook

Reputation: 648

Is it possible to use the Google Drive API with Spreadsheet API

Is there a way to tie Spreadsheets API in with Drive API? By which I mean, if I have a file ID from Google Drive can I then switch to the Spreadsheets API using the same ID and the same oAuth credentials?

I am using php and there is precious little resources especialy for the spreadsheet api - I'm suprised there's so little around.

Upvotes: 3

Views: 1357

Answers (2)

Giles B
Giles B

Reputation: 411

Here's the solution I came up with:

  1. Get both the google-api-php-client library AND the Zend GData library (Zend Framework 1.2, with dependencies).
  2. Use the Google API Google_Oauth2Service class to handle login and authentication. Be sure that your "scopes" includes https://www.googleapis.com/auth/drive AND https://spreadsheets.google.com/feeds .
  3. Once the oauth2 handshake is finished (the details of which you can find documentation for), you have a token, which is a json-encoded string. Turn this into a PHP object: $tok = json_decode([token string])
  4. And here's the magic: just give the access piece of the token to the Zend library

$authsub = Zend_Gdata_AuthSub::getHttpClient($tok->access_token);
$spreadsheet_service = new Zend_Gdata_Spreadsheets($authsub);

Now you can use your spreadsheet service as documented in the Zend library. Works great.

Upvotes: 2

Burcu Dogan
Burcu Dogan

Reputation: 9213

You additionally need to grant permission for user for the following scope:

https://spreadsheets.google.com/feeds

The resources IDs are identical on both APIs. My spreadsheet file on Drive, identified with 0AifSjdPVs9MZdE10eXZTVHVneW9aYjJGVFMxV0VYUEE is accessible on Spreadsheets API with the same ID:

https://spreadsheets.google.com/feeds/spreadsheets/0AifSjdPVs9MZdE10eXZTVHVneW9aYjJGVFMxV0VYUEE

Note: I made the spreadsheet publicly readable, you can try out Spreadsheets API backends with this file.

Upvotes: 3

Related Questions