3
votes

I've read all topicks about this error, but still i have no idea what's wrong... So, i just need to get list of user emails.

In API Console i've created a project and credentials. Also i have api key...

In Dashboard i have Gmail API enabled: http://joxi.ru/1A5bJvMhKo58Br

In my app after accepting permission i've got this error:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission" } ], "code": 403, "message": "Insufficient Permission" } } ' in /home/itindu04/it-industry.com.ua/tlso/api/src/Google/Http/REST.php:118 Stack trace: #0 /home/itindu04/it-industry.com.ua/tlso/api/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /home/itindu04/it-industry.com.ua/tlso/api/src/Google/Task/Runner.php(181): call_user_func_array(Array, Array) #3 /home/itindu04/it-industry.com.ua/tlso/api/src/Google/Http/REST.php(58): Google_Task_Runner->run() #4 /home/itindu04/it-industry.com.ua/tlso/api/src/Google/Client.php(781): Google_Http_REST::execute(Object(GuzzleH in /home/itindu04/it-industry.com.ua/tlso/api/src/Google/Http/REST.php on line 118

Here is the code for get user's msgs:

    require_once __DIR__ . '/api/vendor/autoload.php';
session_start();
$clientId = 'Client ID FROM API CONSOLE';
$clientSecret = 'CLIENT SECRET FROM API CONSOLE';
$redirectUri = 'http://' . $_SERVER['HTTP_HOST'] .'/index.php';

$client = new Google_Client();
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->setDeveloperKey('DEV KEY FROM API CONSOLE');
//$client->addScope('https://mail.google.com/');
//$client->setAuthConfig('client_secret.json');
$client->setScopes(array(
    'https://mail.google.com/',
    'https://www.googleapis.com/auth/gmail.compose'
));
$service = new Google_Service_Gmail($client);
if (isset($_REQUEST['logout'])) {
    unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($url, FILTER_VALIDATE_URL));
}

if (isset($_SESSION['access_token'])) {
    $client->setAccessToken($_SESSION['access_token']);
    $drive_service = new Google_Service_Drive($client);
    $files_list = $drive_service->files->listFiles(array())->getItems();
    echo json_encode($files_list);
} else {
    $loginUrl = $client->createAuthUrl();
    echo 'Click <a href="' . $loginUrl . '">HERE</a> to login';
}

try {
    if (isset($_SESSION['access_token']) && $client->getAccessToken()) {

        $messages = $service->users_messages->listUsersMessages('me',['maxResults'=>2, 'labelids'=> 'INBOX']);
        print_r($messages);
        exit();

    }
} catch (Google_Auth_Exception $e) {
    $loginUrl = $client->createAuthUrl();
    echo 'Error: ' . $e->getMessage;
    echo '<br>Click <a href="' . $loginUrl . '">HERE</a> to re-login';
}
1

1 Answers

0
votes

Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe.

To fix this:

  • Go to the "Less secure apps" section in My Account.
  • Next to "Access for less secure apps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)