简体   繁体   English

直接下载Google Drive API

[英]Direct download google drive api

Good Morning, 早上好,

I'm having to develop a system for downloading a file from google drive direct, but the problem is that I did not know php so I was mounting part by part and so was creating form but, I arrived at a time that I can not solve the following problem, if anyone can help me ... 我必须开发一个用于从Google Drive Direct直接下载文件的系统,但是问题是我不知道php,所以我正在逐个安装,因此正在创建表格,但是,我到达的时候我无法解决以下问题,如果有人可以帮助我...

When I try to download the file, the following error appears: 当我尝试下载文件时,出现以下错误:

An error occurred1: Error refreshing the OAuth2 token, message: '{ "error" : "invalid_client", "error_description" : "The OAuth client was not found." }'

I only have one code page for this to work, and it's just this: 我只有一个代码页可以工作,就是这样:

    <?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();
  }
?>

Can anyone help me in solving this error? 谁能帮助我解决这个错误? It's been a while since I'm behind the answer but to this but I can not find 已经有一段时间了,因为我一直落后于答案,但是对此却找不到

IMAGEM

It seems that you using ClientId in $SERVICE_ACCOUNT_EMAIL instead of right SERVICE_ACCOUNT_EMAIL that must be something like id-123@inductive-world-123456.iam.gserviceaccount.com . 似乎您在$SERVICE_ACCOUNT_EMAIL使用ClientId而不是对SERVICE_ACCOUNT_EMAIL的正确使用,该id-123@inductive-world-123456.iam.gserviceaccount.com必须类似于id-123@inductive-world-123456.iam.gserviceaccount.com And than you must also set the ClientId: 并且还必须设置ClientId:

$client->setClientId($clientId);

So the code will be like this: 因此,代码将如下所示:

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM