我已经阅读了有关此错误的所有热门话题,但我仍然不知道出了什么问题...... 所以,我只需要获取用户电子邮件列表。
在API控制台中,我创建了一个项目和凭据。我也有api钥匙......
在仪表板中,我启用了Gmail API:http://joxi.ru/1A5bJvMhKo58Br
在接受许可后我的应用中出现了这个错误:
致命错误:未捕获的异常'Google_Service_Exception',消息'{“错误”:{“errors”:[{“domain”:“global”,“reason”:“insufficientPermissions”,“message”:“权限不足” },“code”:403,“message”:“权限不足”}}'在/home/itindu04/it-industry.com.ua/tlso/api/src/Google/Http/REST.php:118 Stack跟踪:#0 /home/itindu04/it-industry.com.ua/tlso/api/src/Google/Http/REST.php(94):Google_Http_REST :: decodeHttpResponse(对象(GuzzleHttp \ Psr7 \ Response),Object( GuzzleHttp \ Psr7 \ Request),'Google_Service _...')#1 [内部函数]:Google_Http_REST :: doExecute(对象(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 ::执行(对象(第47行/home/itindu04/it-industry.com.ua/tlso/api/src/Google/Http/REST.php中的GuzzleH
以下是获取用户信息的代码:
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';
}
答案 0 :(得分:0)
Google可能会阻止某些不使用现代安全标准的应用或设备的登录尝试。由于这些应用和设备更容易入侵,因此阻止它们有助于确保您的帐户安全。
解决此问题: