[英]Direct download google drive api
早上好,
我必须开发一个用于从Google Drive Direct直接下载文件的系统,但是问题是我不知道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();
}
?>
谁能帮助我解决这个错误? 已经有一段时间了,因为我一直落后于答案,但是对此却找不到
似乎您在$SERVICE_ACCOUNT_EMAIL
使用ClientId
而不是对SERVICE_ACCOUNT_EMAIL
的正确使用,该id-123@inductive-world-123456.iam.gserviceaccount.com
必须类似于id-123@inductive-world-123456.iam.gserviceaccount.com
。 并且还必须设置ClientId:
$client->setClientId($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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.