【问题标题】:Get image URL instead of content from google drive api从 google drive api 获取图像 URL 而不是内容
【发布时间】:2017-12-09 18:09:56
【问题描述】:

尝试从 Google Drive API 获取图片 URL 而不是文件内容。 在新版本中,这个 GOOD 的东西已被弃用。

我搜索了很多,在谷歌开发者类上有一个方法,但是在新版本中也没有。

问题是我要从云端硬盘中获取大量图片,但又不想降低 Google 尝试通过我的应用浏览所有图片的速度。这也很愚蠢。

有没有办法以某种方式获取这些链接?

这是我用来获取文件内容的代码:

$this->client = new \Google_Client();
$this->client->setAuthConfigFile('config.json');
$this->client->setScopes('https://www.googleapis.com/auth/drive');


if (isset($_GET['code'])) $code = $_GET['code'];

if(!isset($_SESSION['access_token'])){
    if (!isset( $code )){
        $auth_url = $this->client->createAuthUrl();
        $filtered_url = filter_var($auth_url, FILTER_SANITIZE_URL);
        return redirect($filtered_url);
    }else{
        $this->client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $this->client->getAccessToken();
        // return redirect('/');
    }
}

$this->service = new \Google_Service_Drive($this->client);
$file_id = '0B3vR4cBcxn4oNm9TSlBzcngyMzQ';
$results = $this->service->files->get($file_id, array('alt' => 'media'));

$imaga = $results->getBody()->getContents();

$imageData = base64_encode($imaga);
$contentType = $results->getHeader("content-type");

$src = 'data: '.$contentType[0].';base64,'.$imageData;

【问题讨论】:

    标签: php html node.js google-drive-api


    【解决方案1】:

    到目前为止,我认为您可以通过两种方式使用 API 访问您的云端硬盘图像。如果要下载文件,则使用webContentLink;如果要显示文件,则使用webViewLink。这些是您可以使用的可用驱动器metadata。

    【讨论】:

      【解决方案2】:

      我在使用 node.js 和 npm 包googleapis(来自谷歌的官方包)时遇到了同样的问题。

      以下解决方案不在 PHP 中,但概念应保持不变。

      虽然我迟到了,但它可能会帮助其他人寻找类似的问题。

      Node.js

      var google = require('googleapis');
      var OAuth2 = google.auth.OAuth2;
      
      //setup your oauth credentials and tokens
      
      oauth2Client.setCredentials(tokens);
      
      var drive = google.drive({
          version: 'v2',
          auth: oauth2Client
      });
      
      drive.files.get({
          fileId: fileId, //id of the file you are looking for
          alt: 'media'
      }, {
          responseType: 'arraybuffer',
          encoding: null
      }, function(err, response) {
          if (err) {
              console.log(err);
      
              //handle the error
          } else {
              var imageType = response.headers['content-type'];
              var base64 = new Buffer(response.data, 'utf8').toString('base64');
              var dataURI = 'data:' + imageType + ';base64,' + base64;
      
              res.send(dataURI);
          }
      });
      

      HTML

      <img src="dataURI_received_from_above_HTTP_request"></img>
      

      【讨论】:

        猜你喜欢
        • 2019-12-17
        • 1970-01-01
        • 1970-01-01
        • 2021-08-06
        • 2020-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多