I have been trying to use Google's Apps Script API. I have been trying to use Google's Apps Script API. The following are the steps I took so far:
- Ran composer and installed the required package
google/apiclient:^2.0
- Opened a trial Google cloud account
- Enabled the API in the account
- Downloaded and installed gloud cli
- Executed "gcloud install" and chose the project
- Executed "gcloud auth application-default login" and logged into my Google cloud account
When I ran example script it borked with the fatal error in the title.
This is Google's code
require __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
use Google\Client;
use Google\Service\Script;
/**
* Returns an authorized API client.
* @return Client the authorized client object
*/
function getClient()
{
$client = new Client();
$client->setApplicationName('Google Apps Script API PHP Quickstart');
$client->setScopes("https://www.googleapis.com/auth/script.projects");
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
/**
* Shows basic usage of the Apps Script API.
*
* Call the Apps Script API to create a new script project, upload files to the
* project, and log the script's URL to the user.
*/
$client = getClient();
$service = new Script($client);
// Create a management request object.
try{
$request = new Script\CreateProjectRequest();
$request->setTitle('My Script');
$response = $service->projects->create($request);
$scriptId = $response->getScriptId();
$code = <<<EOT
function helloWorld() {
console.log('Hello, world!');
}
EOT;
$file1 = new Script\ScriptFile();
$file1->setName('hello');
$file1->setType('SERVER_JS');
$file1->setSource($code);
$manifest = <<<EOT
{
"timeZone": "America/New_York",
"exceptionLogging": "CLOUD"
}
EOT;
$file2 = new Script\ScriptFile();
$file2->setName('appsscript');
$file2->setType('JSON');
$file2->setSource($manifest);
$request = new Script\Content();
$request->setScriptId($scriptId);
$request->setFiles([$file1, $file2]);
$response = $service->projects->updateContent($scriptId, $request);
echo "https://script.google.com/d/" . $response->getScriptId() . "/edit\n";
}
catch(Exception $e) {
// TODO(developer) - handle error appropriately
echo 'Message: ' .$e->getMessage();
}
PHP's stack trace:
Stack trace:
#0 apps-script/quickstart.php(21): Google\Client->setAuthConfig()
#1 apps-script/quickstart.php(72): getClient()
#2 {main}
thrown in apps-script/vendor/google/apiclient/src/Client.php on line 982
I ran a search on my computer and was unable to find a file called "credentials.json."
Are there any steps I didn't take? The only difference is my version of the apiclient is newer.
CodePudding user response:
You need to create the file from your Google account and then copy it into your project. Steps to be taken can be found here. The methods available are:
The credentials required depends on the type of data, platform, and access methodology of your app. Below are the available credential types:
API key – Use this credential to access publicly-available data anonymously in your app.
OAuth client ID – Use this credential to authenticate as an end user and access their data. Requires your app to request and receive consent from the user.
Service account – Use this credential to authenticate as a robot service account or to access resources on behalf of Google Workspace or Cloud Identity users through domain-wide delegation.