无法使用php将文件上传到Google云端硬盘

时间:2015-08-18 08:07:02

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

我想将文件上传到Google云端硬盘但无法上传。我有产品名称:例如demo_google_drive_app_v1,客户端ID和客户端密钥,我使用以下代码,但我收到错误,我想我确实没有得到正确的sdk上传文件到谷歌驱动器i& #39;我是php和谷歌驱动器的新手,并搜索了很多答案,但无法得到它。现在我收到以下错误消息, 如果有人帮助我解决我的问题,我将感激不尽。

  

致命错误:Class' Config'找不到   C:\ WAMP \ WWW \ upload_drive \谷歌驱动器的PHP-API .... \ SRC \谷歌\ Client.php   在第80行

<?php
/*
 * Simplified version of quickstart.php found on http://developers.google.com/drive/quickstart-php
 *
 */
require_once 'google-api-php/src/Google/Client.php';
require_once 'google-api-php/src/Google/Service.php';
$client = new Google_Client();


$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('XXXXXXXXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXXXXXXXXXXXXX');
$client->setRedirectUri('');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

session_start();

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_Service_Drive($client);

    //Insert a file
    $file = new Google_Service_Drive_DriveFile();
    $file->setTitle(uniqid().'.jpg');
    $file->setDescription('A test document');
    $file->setMimeType('image/jpeg');

    $data = file_get_contents('a.jpg');

    $createdFile = $service->files->insert($file, array(
          'data' => $data,
          'mimeType' => 'image/jpeg',
          'uploadType' => 'multipart'
        ));

    print_r($createdFile);

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

1 个答案:

答案 0 :(得分:1)

我认为你应该在顶部:

 require_once 'google-api-php-client/src/Google/autoload.php';

或手动添加Config.php

require_once(path_to/Config.php)

在此处查看:https://github.com/google/google-api-php-client

更简单的方法是在顶部包含自动加载,如安装说明中所示:

require_once 'google-api-php-client/src/Google/autoload.php';