asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
543 stars 154 forks source link

Service connection - missing access token #110

Closed Dartui closed 8 years ago

Dartui commented 8 years ago

Is there any way to get service account access token via .json file? I am using this example (https://github.com/google/google-api-php-client/blob/master/examples/service-account.php) to connect with OAuth, but I cannot get in any way token. My code:

use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;

include_once 'vendor/autoload.php';
include_once "vendor/google/apiclient/examples/templates/base.php";

$client = new Google_Client();

if ($credentials_file = checkServiceAccountCredentialsFile()) {
    $client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
    $client->useApplicationDefaultCredentials();
} else {
    echo missingServiceAccountDetailsWarning();
    exit;
}

$client->setScopes(array('https://spreadsheets.google.com/feeds'));

// I am passing whole class because I have no idea from where can I get token
$serviceRequest = new DefaultServiceRequest($client);
ServiceRequestFactory::setInstance($serviceRequest);
// This is giving me error: Catchable fatal error: Object of class Google_Client could not be converted to string in /var/www/html/sheets/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php on line 221
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();

Here is class inside $client:

Google_Client Object
(
    [auth:Google_Client:private] => 
    [http:Google_Client:private] => 
    [cache:Google_Client:private] => 
    [token:Google_Client:private] => 
    [config:Google_Client:private] => GuzzleHttp\Collection Object
        (
            [data:protected] => Array
                (
                    [application_name] => 
                    [base_path] => https://www.googleapis.com
                    [client_id] => MY_SERVICE_ID
                    [client_secret] => 
                    [redirect_uri] => 
                    [state] => 
                    [developer_key] => 
                    [use_application_default_credentials] => 1
                    [hd] => 
                    [prompt] => 
                    [openid.realm] => 
                    [include_granted_scopes] => 
                    [login_hint] => 
                    [request_visible_actions] => 
                    [access_type] => online
                    [approval_prompt] => auto
                    [retry] => Array
                        (
                        )

                    [client_email] => MY_SERVICE_MAIL
                    [signing_key] => -----BEGIN PRIVATE KEY-----
MY_SERVICE_KEY
-----END PRIVATE KEY-----

                    [signing_algorithm] => HS256
                )

        )

    [logger:Google_Client:private] => 
    [deferExecution:Google_Client:private] => 
    [requestedScopes:protected] => Array
        (
            [0] => https://spreadsheets.google.com/feeds
        )

)

I am using latest version of OAuth and that's why I cannot use autorization with .p12 file because of this:

Fatal error: Uncaught exception 'BadMethodCallException' with message 'This function no longer exists. See UPGRADING.md for more information' in /var/www/html/sheets/vendor/google/apiclient/src/Google/Client.php:492 Stack trace: #0 /var/www/html/sheets/index.php(19): Google_Client->getAuth() #1 {main} thrown in /var/www/html/sheets/vendor/google/apiclient/src/Google/Client.php on line 492
asimlqt commented 8 years ago

Yes you can and I have used this in the past however it is unrelated to this project. Please refer to the official google php client sdk.

Dartui commented 8 years ago

For everyone who is looking for how to get access token for service by .json file:

include_once 'vendor/autoload.php';
// required for checkServiceAccountCredentialsFile()
include_once "vendor/google/apiclient/examples/templates/base.php";

$client = new Google_Client();

if ($credentials_file = checkServiceAccountCredentialsFile()) {
    $client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
    // this will load service json file, which should be named service-account-credentials.json
    // in my application it is in vendor/google/apiclient folder
    $client->useApplicationDefaultCredentials();
} else {
    echo missingServiceAccountDetailsWarning();
    exit;
}

$token = $client->fetchAccessTokenWithAssertion();
// print_r of $token
// Array ( [access_token] => ACCESS_TOKEN [token_type] => Bearer [expires_in] => 3600 )