谷歌驱动器API 403缺乏权限错误(我是否需要谷歌工作帐户?)

时间:2016-09-13 20:39:19

标签: php api google-drive-api

我想创建一个应用程序,在Google云端硬盘中的这些文件夹中添加文件夹和数据。

我使用refresh_token值成功创建了一个acccess令牌。我试图做一个非常基本的文件上传,但它失败了。我也得到一个文件列表,只是为了看到其他任何工作,当然是。

出现文件列表后,我收到以下错误...

  

致命错误:未捕获的异常' Google_Service_Exception'同   消息'调用POST时出错   https://www.googleapis.com/upload/drive/v3/files?uploadType=media:   (403)许可不足'在   /var/www/html/google-api-php-client/src/Google/Http/REST.php:110

     

堆栈跟踪:#0   /var/www/html/google-api-php-client/src/Google/Http/REST.php(62):   Google_Http_REST :: decodeHttpResponse(对象(Google_Http_Request)   对象(Google_Client))   1 [内部函数]:Google_Http_REST :: doExecute(Object(Google_Client),   对象(Google_Http_Request))

     

2 /var/www/html/google-api-php-client/src/Google/Task/Runner.php(174):   call_user_func_array(Array,Array)

     

3 /var/www/html/google-api-php-client/src/Google/Http/REST.php(46):Google_Task_Runner-> run()

     

4 /var/www/html/google-api-php-client/src/Google/Client.php(593):Google_Http_REST :: execute(Object(Google_Client),   对象(Google_Http_Request))

     

5 /var/www/html/google-api-php-client/src/Google/Service/Resource.php(240):   Google_Client->执行(对象(Google_H in。)   /var/www/html/google-api-php-client/src/Google/Http/REST.php上线   110

更新:我尝试了这一点,但没有使用refresh_token,只是使用最新的访问令牌,但仍然得到完全相同的错误。

最初设置访问权限以进入同意屏幕...

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

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setRedirectUri('http://www.example.com/oauth2callback.php');


if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    print_r($_SESSION);
    $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 {
    $redirect_uri = 'http:/www.example.com/google-api-php-client/oauth2callback.php';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

然后尝试上传文件

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

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');



$client->setAccessType('offline');
$client->setApprovalPrompt('force');

// this token is obtained the first time from refresh_token, it will not be shown after the initial requrest
$refreshToken ='xxx';

$client->refreshToken($refreshToken);
$service = new Google_Service_Drive($client);
$files_list = $service->files->listFiles(array())->getFiles();
echo '<pre>';
print_r($files_list);
echo '</pre>';
$client->addScope("https://www.googleapis.com/auth/drive");

DEFINE("TESTFILE", 'testfile-small.txt');
if (!file_exists(TESTFILE)) {
    $fh = fopen(TESTFILE, 'w');
    fseek($fh, 1024 * 1024);
    fwrite($fh, "!", 1);
    fclose($fh);
}

if ($client->getAccessToken()) {
    // This is uploading a file directly, with no metadata associated.
    $file = new Google_Service_Drive_DriveFile();
    $result = $service->files->create(
        $file,
        array(
            'data' => file_get_contents(TESTFILE),
            'mimeType' => 'application/octet-stream',
            'uploadType' => 'multipart'
        )
    );


}

0 个答案:

没有答案