使用Google API帐户登录并获取所有个人资料信息

时间:2018-03-21 07:19:46

标签: php

当我尝试使用Google帐户登录并获取该登录信息的信息但我收到错误。

这是我的代码,

error_reporting(E_ALL);
ini_set('display_errors',1);
require_once __DIR__.'/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfig('client_secret_gmail.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
 $_SESSION['access_token'];
 echo "<pre>";
 print_r($_SESSION['access_token']);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
  $drive = new Google_Service_Drive($client);
  $files = $drive->files->listFiles(array())->getItems();
  echo json_encode($files);
} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/api/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
} 

,回复是,

Fatal error:  Uncaught Google_Service_Exception: {
 "error": {
  "errors": [
     {
        "domain": "global",
        "reason": "insufficientPermissions",
        "message": "Insufficient Permission"
     }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}

1 个答案:

答案 0 :(得分:0)

您正在申请METADATA权限。你需要DRIVE_READONLY而不是DRIVE_METADATA_READONLY

$client->addScope(Google_Service_Drive::DRIVE_READONLY );

仅供参考google official documentation