asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
544 stars 152 forks source link

Service Account Access #36

Closed machadoug closed 9 years ago

machadoug commented 9 years ago

Greetings,

This is not really an issue, more like a question regarding Service Account Access. Can you provide an example on how to use this php-google-spreadsheet-client with Service Account Access?

I've downloaded the Google APIs Client Library for PHP and I got the service-account.php example working, but I could not manage to get it working with the Spreadsheet example provided.

I'm trying to accomplish the same thing we had with the zend_Gdata, adding a new row for each form submission in our site.

Thanks in advanced for your help.

ZurabWSG commented 9 years ago

This is just an example of how you can authenticate with OAuth 2.0 with Google:

<?php class Google { var $data;

function getToken()
{
    $client = new Google_Client();
    $client->setApplicationName(GOOGLE_APPLICATION_NAME);
    $client->setClientId(GOOGLE_CLIENT_ID);

    $key = file_get_contents(GOOGLE_KEY_FILE);
    $cred = new Google_Auth_AssertionCredentials(
        GOOGLE_CLIENT_EMAIL,
        array(GOOGLE_SPREADSHEETS_SCOPE),
        $key
    );

    $client->setAssertionCredentials($cred);

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

    $service_token = json_decode($client->getAccessToken());
    return $service_token->access_token;
}

function getSpreadsheetsList()
{
    $accessToken        = $this->getToken();

    $serviceRequest     = new DefaultServiceRequest($accessToken);
    ServiceRequestFactory::setInstance($serviceRequest);

    $spreadsheetService = new SpreadsheetService();
    $spreadsheetFeed    = $spreadsheetService->getSpreadsheets();

    print_r($spreadsheetFeed);

    foreach($spreadsheetFeed as $item) {
        $spreadsheets[basename($item->getId())] = $item->getTitle();
    }

    $this->data->spreadsheets = isset($spreadsheets) ? $spreadsheets : false;
}

} ?>

ZurabWSG commented 9 years ago

This script only returns spreadsheets shared explicitly with user with service account email. This is an issue which I'm not able to resolve at this point. Any suggestions would be highly appreciated.

machadoug commented 9 years ago

@ZurabWSG, Thanks! I've managed to list the Spreadsheets using the Service Account, even though there's nothing to list! I've been doing some reading and it seems when accessing with the Service Account you can only access data/resources shared with this service account, unless you are using Google Apps for Business;

I only need to access spreadsheets shared with this Service Account, so it works for me! ;-)

asimlqt commented 9 years ago

I'm glad it's working for you.

I've not used Service Account access so i can't really help you, but there are certain conditions when using the google apis from Service Account.

ZurabWSG commented 9 years ago

@machadoug unless you are using Google Apps for Business -- this helped me, so thank you too!

SimonEast commented 9 years ago

I have created a wiki page which explains how to do this. Hope that helps others. :-)

https://github.com/asimlqt/php-google-spreadsheet-client/wiki/How-to-use-%22Service-account%22-authorization-(rather-than-user-based-access-refresh-tokens)

qualutions commented 9 years ago

Thanks a ton for the wiki page solved my problem.

axelwehner commented 9 years ago

Thanks @SimonEast. That helped me a lot.