亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Google Drive API 下載 jpg

Google Drive API 下載 jpg

PHP
慕碼人2483693 2023-04-15 14:10:36
我正在嘗試使用 php 將 google drive jpg 下載到服務器,以便將其保存為可用的 jpg,例如手動下載時。我可以從 filemeta 獲取 getWebContentLink 并保存擴展名為 .jpg 的“a”文件:$filemeta = $this->service->files->get($id,[    "fields"=>"*"]);$filename = public_path().'/test.jpg';$url=$filemeta->getWebContentLink();file_put_contents($filename, fopen($url, 'r'));然而,這不是真正的 jpg,確實無法使用<img src="/test.jpg" />元素顯示。設置:public function __construct() {        $this->getClient();        $this->service= new Google_Service_Drive($this->client);    }    /**     * @return Google_Client     * @throws \Google_Exception     */    protected function getClient() {        $client = new Google_Client();        $client->setApplicationName('Google Drive API PHP Quickstart');        //20200521$client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY);       $client->setScopes(Google_Service_Drive::DRIVE_READONLY);       // https://www.googleapis.com/auth/drive.readonly        $client->setAuthConfig(base_path().'/apicredentials/google/credentials.json');        $client->setAccessType('offline');        $client->setPrompt('select_account consent');        // Load previously authorized token from a file, if it exists.        // The file token.json stores the user's access and refresh tokens, and is        // created automatically when the authorization flow completes for the first        // time.        $tokenPath = base_path().'/apicredentials/google/token.json';        if (file_exists($tokenPath)) {            $accessToken = json_decode(file_get_contents($tokenPath), true);            $client->setAccessToken($accessToken);        }        // If there is no previous token or it's expired.        if ($client->isAccessTokenExpired()) {            // Refresh the token if possible, else fetch a new one.            if ($client->getRefreshToken()) {                $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());            }                 }            }
查看完整描述

1 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

這個答案怎么樣?

問題和解決方法:

為了webContentLink從外部檢索文件內容,需要使用訪問令牌或文件需要公開共享。所以在你的腳本中,我認為創建的文件是登錄頁面的 HTML 數據。

那么以下解決方法如何?

  1. webContentLink使用訪問令牌下載文件內容。

  2. 首先,在 Google Drive 上公開共享該文件。然后,使用您當前的腳本下載文件內容。

  3. 使用 Drive API 直接下載文件內容。在這種情況下,修改后的腳本如下。如果您使用https://www.googleapis.com/auth/drive.metadata.readonlyas 范圍,請修改為https://www.googleapis.com/auth/drive.readonly.

$content = $this->service->files->get($id, array("alt" => "media"));

$filename = public_path().'/test.jpg';

file_put_contents($filename, $content->getBody());


查看完整回答
反對 回復 2023-04-15
  • 1 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號