【问题标题】:Insufficient Permission Gmail API权限不足 Gmail API
【发布时间】:2017-04-24 14:48:05
【问题描述】:

我已阅读有关此错误的所有主题,但我仍然不知道出了什么问题... 所以,我只需要获取用户电子邮件列表。

在 API 控制台中,我创建了一个项目和凭据。我也有 api 密钥...

在仪表板中我启用了 Gmail API:http://joxi.ru/1A5bJvMhKo58Br

在接受许可后在我的应用中出现此错误:

致命错误:未捕获的异常 'Google_Service_Exception' 带有消息 '{ "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission" } ], "code": 403, "message": "Insufficient Permission" } } ' 在 /home/itindu04/it-industry.com.ua/tlso/api/src/Google/Http/REST.php:118 堆栈跟踪:#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 [内部函数]: 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(Ob ject(GuzzleH 在 /home/itindu04/it-industry.com.ua/tlso/api/src/Google/Http/REST.php 第 118 行

获取用户信息的代码如下:

    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';
}

【问题讨论】:

    标签: gmail-api


    【解决方案1】:

    Google 可能会阻止来自不使用现代安全标准的某些应用或设备的登录尝试。由于这些应用和设备更容易被入侵,因此阻止它们有助于确保您的帐户安全。

    解决这个问题:

    • 转到“我的帐户”中的 "Less secure apps" 部分。
    • 在“访问安全性较低的应用程序”旁边,选择打开。 (Google Apps 用户注意:如果您的管理员锁定了不太安全的应用帐户访问权限,则此设置会隐藏。)

    【讨论】:

    • hm...这个选项在使用帐号时已经启用...但是还是一样的错误
    • 也许我应该启用其他一些 API 以使其工作?
    • 这个响应是错误的,google api 必须在“不太安全的应用程序”关闭的情况下工作
    猜你喜欢
    • 2015-08-20
    • 2019-08-16
    • 1970-01-01
    • 2015-10-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2016-02-18
    相关资源
    最近更新 更多