当前位置: 首页 > 知识库问答 >
问题:

谷歌-api-php-客户端 - 驱动器 - 文件插入 - 创建文本文件 - 未添加的内容

邹嘉荣
2023-03-14

我想创建一个纯/文本哑剧类型文件,使用PHP和谷歌应用程序引擎上的谷歌驱动器的谷歌API。

当我使用:

'mimeType' => 'text/plain',

没有内容写入文件。

如果我使用:

'mimeType' => 'application/octet-stream',
'uploadType' => 'media'

内容将添加到文件中。

这段代码来自Google文档:

显示“文本/纯”类型的 mime 类型的代码行

'mimeType' => 'text/plain',

但是,同样,如果我使用它,则不会向文件中添加任何内容。创建纯文本文件和添加内容的设置是什么?

我的完整代码是这样的:

<?php
/* You can NOT see the files created in a Service Account without doing some special stuff.  You need to set some kind
of permissions, and then you can see the files in your SHARED WITH ME section of Google Drive 
http://stackoverflow.com/questions/25302794/google-drive-api-services-account-view-uploaded-files-to-google-drive-using-java*/
session_start();

require_once realpath(dirname(__FILE__) . '/google-api-php-client/src/Google/autoload.php');

$client_id = 'your Client ID'; //Client ID
$service_account_name = 'account name'; //Email Address
$key_file_location = 'fileName.p12'; //key.p12

$theDateTime = date("Y-m-d h:i:sa");
echo "indexService - " . $theDateTime . '<br>';

$client = new Google_Client();
$client->setClientId($client_id);

//$client->addScope("https://www.googleapis.com/service/drive");
$service = new Google_Service_Drive($client);

if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
//echo '$key: ' . $key. '<br>';

$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/drive'),
    $key
);

$client->setAssertionCredentials($cred);

if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

//Begin of code that is same for both service and web

//  If signed in, upload
if ($client->getAccessToken()) {
  echo 'Got access token <br>';

  $fileData = "This is the file Content: " . $theDateTime;
  $fileName = 'User123_Date';
  echo '$fileName: ' . $fileName . '<br>';
  echo '$fileData: ' . $fileData . '<br>';

  //Upload file with metadata
  $file = new Google_Service_Drive_DriveFile();
  $file->setTitle($fileName);
  $result2 = $service->files->insert(
      $file,
      array(
        'data' => $fileData,
        'mimeType' => 'application/octet-stream',
        'uploadType' => 'media'
      )
  );

  $theId = $result2->getId();
  $downloadUrl = $result2->getDownloadUrl();
  echo 'the ID: ' . $theId . '<br>';
  echo "Title: " . $result2->getTitle() . '<br>';
  echo "MIME type: " . $result2->getMimeType() . '<br>';
  echo '$downloadUrl: ' . $downloadUrl . '<br>';

  // get the file info as a check
    try {
      echo 'try part <br>';
      $result3 = $service->files->get($theId);
      echo "Title: " . $result3->getTitle() . '<br>';
      //echo "Description: " . $result3->getDescription() . '<br>';
      echo "MIME type: " . $result3->getMimeType() . '<br>';
    } catch (Exception $e) {
      echo "An error occurred: " . $e->getMessage() . '<br>';
    };

   if ($downloadUrl) {
     echo 'there is a download url <br>';

    $request = new Google_Http_Request($downloadUrl, 'GET', null, null);
    $httpRequest = $service->getClient()->getAuth()->authenticatedRequest($request);
    if ($httpRequest->getResponseHttpCode() == 200) {
      $theContentIs = $httpRequest->getResponseBody();
      echo '$theContentIs: ' . $theContentIs . '<br>';
    } else {
      // An error occurred.
      echo 'an error occurred getting file contents <br>';
    }
  } else {
    // The file doesn't have any content stored on Drive.
    echo 'The file doesnt have any content stored on Drive. <br>';
  }
}

echo "File Upload - Uploading a small file".'<br>';
?>

<?php
echo 'end of code';

共有1个答案

章茂
2023-03-14

若您想要一个具有该MIME类型的文本文件,请从PHP创建,而不是直接在驱动器上创建。这就是为什么模仿类型没有被识别。fopen()可以做到这一点。

 类似资料:
  • 这是我收到的错误

  • 我试图访问我的用户的google drive内容。我可以使用带有正确参数的google drive用户许可url在我的域名上兑换代码:https://accounts.google.com/o/oauth2/v2/auth?响应类型=代码 我正在尝试向https://www.googleapis.com/oauth2/v4/tokenendpoint执行角$超文本传输协议请求。请求如下所示: 然而

  • 当观察驱动器更改时,我会收到带有新更改ID的通知。但是当我尝试使用:driveservice.changes().get(changeId)查询它时,我断断续续地得到404。我是不是做错什么了? 正在观看文件: 当观察文件更改时,如果是文件夹,我想知道添加到该文件夹的新文件,所以我希望从该文件夹添加/删除文件时,“x-goog-resource-state”将包含“add/remove”值,而“x

  • 我正在使用google drive api来管理多个用户的一些文件。现在我想知道如何将一些文件的所有权转移给另一个用户。