google-api-php-client-驱动器-文件插入-创建文本文件-内容未添加 [英] google-api-php-client - Drive - File Insert - Create a text file - Content not being added

查看:53
本文介绍了google-api-php-client-驱动器-文件插入-创建文本文件-内容未添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PHP和Google App Engine上用于Google云端硬盘的Google-API创建纯文本/文本 Mime Type文件.

I want to create a plain/text Mime Type file, using PHP and the Google-API for Google Drive on Google App Engine.

当我使用时:

'mimeType' => 'text/plain',

没有内容写入文件.

如果我使用:

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

内容已添加到文件中.

此代码来自 Google文档:

显示'text/plain'的mimeType的代码行

Shows a line of code for the mimeType of 'text/plain'

'mimeType' => 'text/plain',

但是,如果我再次使用该文件,则不会将任何内容添加到文件中.创建纯文本文件和添加内容的设置是什么?

But, again, if I use that, no contents are added to the file. What are the settings for creating a plain text file and adding content?

我的完整代码是这样:

<?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';

推荐答案

如果要使用带有该MimeType的文本文件,请不要直接在Drive上通过PHP创建它.这就是为什么这种模仿类型没有得到认可的原因. fopen()可以解决问题.

If you want a text file with that MimeType, create it from PHP not directly on Drive. That's why that mimetype isn't getting recognized. fopen() can do the trick.

这篇关于google-api-php-client-驱动器-文件插入-创建文本文件-内容未添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆