直接下载google驱动api

Direct download google drive api

早上好,

我必须开发一个系统来直接从 google 驱动器下载文件,但问题是我不知道 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 的东西。 而且你还必须设置 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);
}