早上好,
我必须开发一个直接从谷歌驱动器下载文件的系统,但问题是我不知道php所以我一个接一个地安装,所以创建表单但是,我到达了我无法解决以下问题的时间,如果有人可以帮助我...
当我尝试下载文件时,会出现以下错误:
An error occurred1: Error refreshing the OAuth2 token, message: '{ "error" : "invalid_client", "error_description" : "The OAuth client was not found." }'
我只有一个代码页可供使用,而且只是这个:
<?php
require_once "google/google-api-php-client/src/Google_Client.php";
require_once "google/google-api-php-client/src/contrib/Google_DriveService.php";
require_once "google/google-api-php-client/src/contrib/Google_Oauth2Service.php";
require_once "google/vendor/autoload.php";
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$CLIENT_ID = '';
$SERVICE_ACCOUNT_EMAIL = '';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = '';
function buildService() {//function for first build up service
global $DRIVE_SCOPE, $SERVICE_ACCOUNT_EMAIL, $SERVICE_ACCOUNT_PKCS12_FILE_PATH, $CLIENT_ID;
$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
$SERVICE_ACCOUNT_EMAIL,
array($DRIVE_SCOPE),
$key);
$client = new Google_Client();
$client->setUseObjects(true);
$client->setClientId($CLIENT_ID);
$client->setAssertionCredentials($auth);
return new Google_DriveService($client);
}
/**
* Print a file's metadata.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param string $fileId ID of the file to print metadata for.
*/
function printFile($service, $fileId) {
try {
$file = $service->files->get($fileId);
print "Title: " . $file->getTitle();
print "Description: " . $file->getDescription();
print "MIME type: " . $file->getMimeType();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
/**
* Download a file's content.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param File $file Drive File instance.
* @return String The file's content if successful, null otherwise.
*/
function downloadFile($service, $file) {
$downloadUrl = $file->getDownloadUrl();
if ($downloadUrl) {
$request = new Google_HttpRequest($downloadUrl, 'GET', null, null);
$httpRequest = Google_Client::$io->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
// An error occurred.
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
try {
//https://drive.google.com/open?id=0B9ez4Vc-n0DbWkV6VmtRZFJIbnhqU3d2QmNHTTZfWWJYZGM0
$file_id='1cKbjJzSJ4ZcedfFUEe2MwncsDYGRuScl';
$service=buildService();
//printFile($service,$file_id);
$file = $service->files->get($file_id);
header('Content-Type: '.$file->getMimeType());
print(downloadFile($service,$file));
} catch (Exception $e) {
print "An error occurred1: " . $e->getMessage();
}
?>
有人可以帮我解决这个错误吗?已经有一段时间了,因为我的答案背后但是我找不到
答案 0 :(得分:1)
您似乎在ClientId
而不是右$SERVICE_ACCOUNT_EMAIL
使用SERVICE_ACCOUNT_EMAIL
,而id-123@inductive-world-123456.iam.gserviceaccount.com
必须是$client->setClientId($clientId);
。
而且还必须设置ClientId:
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$CLIENT_ID = 'fulasdaasd40dbsdfssdfam.gserviceaccount.com';
$SERVICE_ACCOUNT_EMAIL = 'id-123@inductive-world-123456.iam.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'My-File.p12';
function buildService()
{
global $DRIVE_SCOPE, $SERVICE_ACCOUNT_EMAIL, $SERVICE_ACCOUNT_PKCS12_FILE_PATH, $CLIENT_ID;
$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
$SERVICE_ACCOUNT_EMAIL,
array($DRIVE_SCOPE),
$key);
$client = new Google_Client();
$client->setUseObjects(true);
$client->setClientId($CLIENT_ID);
$client->setAssertionCredentials($auth);
return new Google_DriveService($client);
}
所以代码将是这样的:
div