I'm working on a project requiring to get the events from connected user. I use HWIO so my users can only login using Google account. But when I tried to get the events, I get the event of the account I used to create the project, and not those from logged in users. So I tried to redo the code using an other doc, but when I access the page I get an "Invalid code" error
I tried to change credential.json file and I think it is correct as I could retrived my main user's info with it before
The controller's code to get the event :
$path = $this->get('kernel')->getRootDir() . '\..\src\AppBundle\Resources\credentials.json';
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
try {
$client->setAuthConfig($path);
} catch (\Google_Exception $e) {
}
$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:
%s
", $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()));
}
// Get the API client and construct the service object.
$service = new Google_Service_Calendar($client);
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
$events = $results->getItems();
I should have the prompt to ask the user to use the api, but I get an error page with raw taxt :
"Open the following link in your browser: https://accounts.google.com/o/oauth2/auth[......]&prompt=select_account%20consent Enter verification code: "
And the symfony error:
"Invalid code"