Answer the question
In order to leave comments, you need to log in
How to make authorization in Google Spreadsheets in PHP?
I've been fumbling at Google's documentation for quite a long time, but I still haven't been able to put together the right picture of how it works, let alone explain why.
I ask for help - I need it to get the rights to write and read the table into an array when requesting the script. In general, I managed to count and write. From the browser. But here the automatic token from the script does not work, and each time it is manually updated - well, it cannot be that Google did this.
Separate concepts:
1. Google account
2. Google Api account
As I understand it works:
1. Create a Google account for any mail
2. In the settings, create a service account for Google Api
3. In the same place, create another Google Api account and select web there -Appendix
4. Download client_secret.json
5. Install a lib from google `google-api-php-client-2.1.1`
6. Write this code from this lib:
// config
$client_secret_fpath = '<fpath to client_secret.json>';
$client_secret = json_decode(file_get_contents($client_secret_fpath), true);
// process
// init client
$client = new Google_Client();
$client->setAuthConfig($client_secret_fpath);
$client->addScope(Google_Service_Sheets::SPREADSHEETS);
$client->setIncludeGrantedScopes(true);
$client->setAccessType('offline');
$token_fpath = __DIR__ . '/token.ini';
if (!is_file($token_fpath) || (!$access_token = json_decode(file_get_contents($token_fpath), true))):
$auth_url = $client->createAuthUrl();
// ... стопэээээ, ни curl, ни wget не спасут, redirect делать нельзя, т.к. терминал
endif;
Answer the question
In order to leave comments, you need to log in
Happened.
In order to gain access from another computer (without user intervention) - the so-called. service accounts. When we create such an account - Google warns that `credentials` will be created in a single instance and it is unacceptable to "lose" them.
Thus, you need to use in the script:
$client_secret_fpath = __DIR__ . 'service-account-credentials.json';
$client->setAuthConfig($client_secret_fpath);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question