未经捕获的异常“Google_Exception”,消息“在经过身份验证后无法添加服务”

时间:2015-05-05 05:36:34

标签: php google-drive-api google-api-php-client

我想在php中使用google drive api添加谷歌电子表格。但我在身份验证后收到以下错误。

 Uncaught exception 'Google_Exception' with message 'Cant add services after having authenticated' in /var/www/html/spreadsheet/google/src/Google_Client.php:119 Stack trace: #0 /var/www/html/spreadsheet/google/src/contrib/Google_DriveService.php(1046): Google_Client->addService('drive', 'v2') #1 /var/www/html/spreadsheet/index.php(20): Google_DriveService->__construct(Object(Google_Client)) #2 {main} thrown in /var/www/html/spreadsheet/google/src/Google_Client.php on line 119

我的代码:

<?php
        require '/var/www/html/spreadsheet/google/src/Google_Client.php';
        require '/var/www/html/spreadsheet/google/src/contrib/Google_DriveService.php';
        require '/var/www/html/spreadsheet/google/src/contrib/Google_Oauth2Service.php';

        $client = new Google_Client();
        $client->setClientId('xxxxx-vdbo5lga3sq7g8q4g8adgqh72m0ng8ef.apps.googleusercontent.com');
        $client->setClientSecret('BCGuyCPHwNflflBU5jDQ25LQ');
        $client->setRedirectUri('http://localhost/spreadsheet/index.php');
        $client->setScopes(array('https://www.googleapis.com/auth/drive'));

      if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
      if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();
      } else
        $client->setAccessToken($_SESSION['access_token']);

        $service = new Google_DriveService($client);

        //Inserting a file
        $file = new Google_DriveFile();
        $file->setTitle('Mysheet');
        $file->setDescription('My first sheet through php');
        $file->setMimeType('application/vnd.google-apps.spreadsheet');

        $createdFile = $service->files->insert($file, array(
              'mimeType' => 'application/vnd.google-apps.spreadsheet',
              'uploadType' => 'multipart'
            ));

        print_r($createdFile);

       } else {
        $authUrl = $client->createAuthUrl();
        header('Location: ' . $authUrl);
        exit();
       }
?>

1 个答案:

答案 0 :(得分:0)

在对客户端进行身份验证之前,您必须创建一项新服务:

<?php
// ...

$client = new Google_Client();
$client->setClientId('clientID');
$client->setClientSecret('clientSecret');
$client->setRedirectUri('redirectURI');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

// create Drive service before authentication
$service = new Google_DriveService($client);


if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();
    } else {
        $client->setAccessToken($_SESSION['access_token']);
    }
    // ...
} else {
    // ...
}

顺便说一句:请参阅google-api-php-client repo中的simplefileupload example