My ultimate goal is to send me an email through the Google Gmail API.
And here is my problem.
When I get an access token, a message appears
Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Wrong number of segments in token: '
I read here the oauth2 endpoint endpoints that βThis does NOT mean your token was invalidβ, but I get a fatal error that interrupts my script.
My access token looks like this: 4 / MqiIIl5K4S3D4iiieHshQt5D4M79oo07SbhMn22oe2o.cswa8t9ZuDAfJvIeHux6iLYXpNQmlAI
If I refresh the page with this token, I would get another error, which
'Error fetching OAuth2 access token, message: 'invalid_grant: Invalid code.'
Here is my code
<?php include_once "templates/base.php"; echo pageHeader("Simple API Access"); require_once realpath(dirname(__FILE__) . '/../autoload.php'); $client = new Google_Client(); $client_id = '114600397795-j5sn0gvsdrup0s8dcmsj49iojp3m9biu.apps.googleusercontent.com'; $client_secret = 'm3Dzankql_rs1OGICsA3Hbtc'; $redirect_uri = 'http://alupa.com/gmail/examples/simple-query.php'; $client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/gmail.readonly"); $client->addScope("https://mail.google.com/"); $apiKey = "AIzaSyCWXxrTshKsotxEYNZZCXxdVXhLeku55cw"; $client->setDeveloperKey($apiKey); if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['access_token'] = $client->getAccessToken(); //$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; //header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); }else{ $client->setApplicationName("Client_Gmail_Examples"); } if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $client->setAccessToken($_SESSION['access_token']); } else { $authUrl = $client->createAuthUrl(); } if ($client->getAccessToken()) { $_SESSION['access_token'] = $client->getAccessToken(); $token_data = $client->verifyIdToken()->getAttributes(); } ?> <div class="box"> <div class="request"> <?php if (isset($authUrl)) { echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; } else { echo "<a class='logout' href='?logout'>Logout</a>"; } ?> </div> </div>
alupa.com is my local domain and I don't see any problems with this
I am using the original library from google https://github.com/google/google-api-php-client
source share