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:
google/apiclient:^2.0
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.
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.