Php retrieveAllFiles函数的参数
我正在尝试使用google drive API函数retrieveAllFiles()显示google drive帐户中的文件列表, 但由于它的参数,我不知道如何调用它。这里是函数Php retrieveAllFiles函数的参数,php,google-drive-api,Php,Google Drive Api,我正在尝试使用google drive API函数retrieveAllFiles()显示google drive帐户中的文件列表, 但由于它的参数,我不知道如何调用它。这里是函数 function retrieveAllFiles($service) { $result = array(); $pageToken = NULL; do { try { $parameters = array();
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
print $pageToken;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
我使用此链接作为参考:
我如何获得google drive API的服务才能通过它?
$service
指的是hasgoogle\u DriveService
,它是google\u ServiceResource
要使用示例脚本,您需要下载完整的
我按照你说的做了,先生,但是我有一个错误:“发生了一个错误:调用GET:(500)Internal ErrorArray时出错”我已经输入了clientid,secret。。等是什么导致了这个错误?
session_start();
require_once 'PATH_TO/src/Google_Client.php';
require_once 'PATH_TO/src/service/Google_ServiceResource.php';
require_once 'PATH_TO/src/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setApplicationName("Google Analytics PHP Starter Application");
$client->setClientId('insert_your_oauth2_client_id');
$client->setClientSecret('insert_your_oauth2_client_secret');
$client->setRedirectUri('insert_your_oauth2_redirect_uri');
$client->setDeveloperKey('insert_your_developer_key');
$service = new Google_DriveService($client);
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_APP_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();
//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = file_get_contents('document.txt'); //Put this file into same folder
$createdFile = $service->files->insert($file, array(
//'data' => $data,
'mimeType' => 'text/plain',
)); ////Upload File
echo '<pre>';
//print_r($createdFile);
echo '</pre>';
//////Function for getting all files from your drive
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files['items']);
$pageToken = $files['nextPageToken'];
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
$listss = retrieveAllFiles($service);
echo '<pre>';
print_r($listss);
echo '</pre>';
?>