Php 直接下载谷歌硬盘api

Php 直接下载谷歌硬盘api,php,google-drive-realtime-api,Php,Google Drive Realtime Api,早上好 我必须开发一个从google drive direct下载文件的系统,但问题是我不懂php,所以我一部分一部分地安装,然后创建表单,但是,我到达的时候,我无法解决以下问题,如果有人能帮我的话 当我尝试下载文件时,出现以下错误: An error occurred1: Error refreshing the OAuth2 token, message: '{ "error" : "invalid_client", "error_description" : "The OAuth clie

早上好

我必须开发一个从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\u ACCOUNT\u EMAIL
中使用了
ClientId
,而不是右
SERVICE\u ACCOUNT\u EMAIL
,后者必须类似于
id-123@inductive-world-123456.iam.gserviceaccount.com
。 此外,还必须设置客户端ID:

$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);
}

@用户3602803您是否修复了服务帐户电子邮件并使用了正确的客户ID?服务电子邮件帐户是否与.p12文件匹配?@user3602803您在代码中混淆了服务电子邮件帐户和客户端ID)它必须是
$CLIENT\u ID='234884101335-5lh61u0m8gh5jbnfurags39vc0neu17u.apps.googleusercontent.com'
$SERVICE\u ACCOUNT\u EMAIL='2!'fullacess@db360-196715.iam.gserviceaccount.com'@user3602803那么您确定.p12文件是为该服务帐户提供的吗?您在此处看到该帐户和在此处创建的密钥了吗?@user3602803您是否与SERVICE_account_EMAIL共享了该帐户?@user3602803在it github上有此库的文档和示例。你也可以在谷歌搜索中找到很多例子)你的问题已经解决了。试着自己做下一步;)这将是有用的)