在 php 中使用 google 驱动器 api 列出所有文件时出错
error on listing all files using google drive api in php
我正在使用 PHP 开发 google 驱动器 api。我在使用此驱动器 api 时遇到错误。我正在使用 google api 客户端库 "google/apiclient ^2.0"。我正在尝试使用 google 身份验证进行用户登录,并为此目的查看驱动器中可用的所有文件,我正在使用此库,但它显示错误:
Notice: Undefined property: Google_Client::$files in
/Applications/XAMPP/xamppfiles/htdocs/google_app/index.php on line 31
Fatal error: Uncaught Error: Call to a member function listFiles() on
null in /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php:31
Stack trace: #0
/Applications/XAMPP/xamppfiles/htdocs/google_app/index.php(55):
retrieveAllFiles(Object(Google_Client)) #1 {main} thrown in
/Applications/XAMPP/xamppfiles/htdocs/google_app/index.php on line 31
include_once 'vendor/autoload.php';
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();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setAccessType("offline"); // offline access
//$client->setIncludeGrantedScopes(true); // incremental auth
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
}
$service = new Google_service_Device();
echo retrieveAllFiles($service );
实际上我正在尝试使用我的文件 ID 检索我的 google 驱动器中的所有文件,然后设置状态自动发布特定文件。请帮助我摆脱这个错误。
提前致谢。
"Daily limit for unauthenticated Use Exceeded"
表示您在未正确验证脚本的情况下发出请求。您的 fetchAccessTokenWithAuthCode 可能有问题。
您可能会考虑使用较长的这些行
function getOauth2Client() {
try {
$client = buildClient();
// Set the refresh token on the client.
if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
$client->refreshToken($_SESSION['refresh_token']);
}
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
// Refresh the access token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
return $client;
} else {
// We do not have access request access.
header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
}
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
从我的示例项目中提取的代码 oauth2php
Undefined property Google_Client
表示您没有正确声明 google 客户端
Uncaught Error: Call to a member function listFiles() on null in
如果删除 $parameters 并只调用 $service->files->listFiles();
会发生什么
我正在使用 PHP 开发 google 驱动器 api。我在使用此驱动器 api 时遇到错误。我正在使用 google api 客户端库 "google/apiclient ^2.0"。我正在尝试使用 google 身份验证进行用户登录,并为此目的查看驱动器中可用的所有文件,我正在使用此库,但它显示错误:
Notice: Undefined property: Google_Client::$files in /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php on line 31
Fatal error: Uncaught Error: Call to a member function listFiles() on null in /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php:31 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php(55): retrieveAllFiles(Object(Google_Client)) #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php on line 31
include_once 'vendor/autoload.php';
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();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setAccessType("offline"); // offline access
//$client->setIncludeGrantedScopes(true); // incremental auth
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
}
$service = new Google_service_Device();
echo retrieveAllFiles($service );
实际上我正在尝试使用我的文件 ID 检索我的 google 驱动器中的所有文件,然后设置状态自动发布特定文件。请帮助我摆脱这个错误。
提前致谢。
"Daily limit for unauthenticated Use Exceeded"
表示您在未正确验证脚本的情况下发出请求。您的 fetchAccessTokenWithAuthCode 可能有问题。
您可能会考虑使用较长的这些行
function getOauth2Client() {
try {
$client = buildClient();
// Set the refresh token on the client.
if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
$client->refreshToken($_SESSION['refresh_token']);
}
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
// Refresh the access token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
return $client;
} else {
// We do not have access request access.
header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
}
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
从我的示例项目中提取的代码 oauth2php
Undefined property Google_Client
表示您没有正确声明 google 客户端
Uncaught Error: Call to a member function listFiles() on null in
如果删除 $parameters 并只调用 $service->files->listFiles();
会发生什么