Reputation: 31
I am a student and now it's the final stage of my final project. I am working with PHP. The documentation that I worked with is: https://developers.google.com/calendar/quickstart/php
My problem is that the Token returns NULL.
The error I get is: PHP Fatal error: Uncaught InvalidArgumentException: invalid json token in C:\wamp64\www\MarryMe\vendor\google\apiclient\src\Google\Client.php:420 Stack trace: #0 C:\wamp64\www\MarryMe\insertevent.php(26): Google_Client->setAccessToken(NULL) #1 C:\wamp64\www\MarryMe\insertevent.php(66): getClient() #2 {main} thrown in C:\wamp64\www\MarryMe\vendor\google\apiclient\src\Google\Client.php on line 420
Here is my code:
require_once('./conection/init.php');
require __DIR__ . '/vendor/autoload.php';
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('MarryMe');
$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
$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 {
if (!isset($_GET['code'])) {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
header("location: $authUrl");
} else {
$code = $_GET['code'];
$authCode = trim($code);
var_dump($authCode);
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
var_dump($accessToken);
$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;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
'maxResults' => 50,
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
$events = $results->getItems();
$event = new Google_Service_Calendar_Event(array(
'summary' => $_SESSION['name'],
'location' => ' ',
'description' => $_SESSION['description'],
'start' => array(
'dateTime' => $_SESSION['start_date'],
'timeZone' => 'UTC',
),
'end' => array(
'dateTime' => $_SESSION['due_date'],
'timeZone' => 'UTC',
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 24 * 60),
array('method' => 'popup', 'minutes' => 10),
),
),
));
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
header("location:./includes/tasksProcess/tasks.php");
//end of insert event ```
Upvotes: 3
Views: 2808
Reputation: 81
Make sure that __DIR__ .
is before where your credentials is instantiated so that the function knows where to look for the json file:
so it should look like this
$client->setAuthConfig( __DIR__ . 'credentials.json');
Upvotes: 0