Google Drive API下载jpg

问题描述 投票:0回答:1

[我正在尝试使用php将google驱动器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());
            } else {
                // Request authorization from the user.
                $authUrl = $client->createAuthUrl();
                printf("Open the following link in your browser:\n%s\n", $authUrl);
                print 'Enter verification code: ';
                $authCode = trim(fgets(STDIN));

                // Exchange authorization code for an access token.
                $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
                $client->setAccessToken($accessToken);

                // Check to see if there was an error.
                if (array_key_exists('error', $accessToken)) {
                    throw new Exception(join(', ', $accessToken));
                }
            }
            // Save the token to a file.
            if (!file_exists(dirname($tokenPath))) {
                mkdir(dirname($tokenPath), 0700, true);
            }
            file_put_contents($tokenPath, json_encode($client->getAccessToken()));
        }
        $this->client=$client;
    }
php download google-drive-api jpeg
1个回答
2
投票

这个答案怎么样?

问题和解决方法:

为了从外部用webContentLink检索文件内容,需要使用访问令牌,或者需要公开共享文件。因此,在您的脚本中,我认为创建的文件是登录页面的HTML数据。

那么以下解决方法如何?

  1. [使用访问令牌通过webContentLink下载文件内容。

  2. 首先,在Google云端硬盘上公开共享文件。然后,使用当前脚本下载文件内容。

  3. 使用Drive API直接下载文件内容。在这种情况下,修改后的脚本如下。如果您使用https://www.googleapis.com/auth/drive.metadata.readonly作为示波器,请修改为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());
    

参考:

© www.soinside.com 2019 - 2024. All rights reserved.