Php 谷歌驱动Api列出照片

Php 谷歌驱动Api列出照片,php,api,google-drive-api,Php,Api,Google Drive Api,我的GoogleDrive api在上传文件方面运行良好。但我很困惑,我怎么能把用户驱动器的文件(尤其是照片)列到我的网站上 仅供参考:为了下载并列出,我复制了MediaFileUpload.php并创建了一个名为MediaFileDownload.php的新文件,然后将其所有功能从%upload%重命名为%download% 下面是我的代码: Index.php <?php /* * Copyright 2011 Google Inc. *

我的GoogleDrive api在上传文件方面运行良好。但我很困惑,我怎么能把用户驱动器的文件(尤其是照片)列到我的网站上

仅供参考:为了下载并列出,我复制了MediaFileUpload.php并创建了一个名为MediaFileDownload.php的新文件,然后将其所有功能从%upload%重命名为%download%

下面是我的代码:

Index.php

    <?php
    /*
     * Copyright 2011 Google Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    include_once "templates/base.php";
    session_start();

    set_include_path("src/" . PATH_SEPARATOR . get_include_path());
    require_once 'Google/Client.php';
    require_once 'Google/Http/MediaFileDownload.php';
    require_once 'Google/Service/Drive.php';

    /************************************************
      ATTENTION: Fill in these values! Make sure
      the redirect URI is to this page, e.g:
      http://localhost:8080/fileupload.php
     ************************************************/
    $client_id = 'Your_Client_Id';
    $client_secret = 'Your_Client_Secret';
    $redirect_uri = 'http://localhost/MyApi/Google-Drive';

    $client = new Google_Client();
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->addScope("https://www.googleapis.com/auth/drive");
    $service = new Google_Service_Drive($client);
    if (isset($_REQUEST['logout'])) {
      unset($_SESSION['download_token']);
    }

    if (isset($_GET['code'])) {
      $client->authenticate($_GET['code']);
      $_SESSION['download_token'] = $client->getAccessToken();
      $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
      header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }

    if (isset($_SESSION['download_token']) && $_SESSION['download_token']) {
      $client->setAccessToken($_SESSION['download_token']);
      if ($client->isAccessTokenExpired()) {
        unset($_SESSION['download_token']);
      }
    } else {
      $authUrl = $client->createAuthUrl();
    }

    /********************************************************
      If we're signed in then lets try to download our file.
     ********************************************************/
if ($client->getAccessToken()) {
    // This is downloading a file directly, with no metadata associated.
    echo "The code is being executed till Line No. 67.";
    /**************************************************************
    **     -----This comment is created by Ashish Shah-----     **
    ** The whole code is working if the user is not logged in.  **
    ** After user loggs in, Nothing is being displayed after    **
    ** this block.                                              **
    **************************************************************
    ** The problem is in Line No.76 to Line No.79               **
    **************************************************************/
    $file = new Google_Service_Drive_DriveFile();
    $result = $service->files->list(
        $file,
        array('downloadType' => 'media')
    );
}

    echo pageHeader("File Download - Downloading a Photo");
    if (
        $client_id == '<YOUR_CLIENT_ID>'
        || $client_secret == '<YOUR_CLIENT_SECRET>'
        || $redirect_uri == '<YOUR_REDIRECT_URI>') {
      echo missingClientSecretsWarning();
    }
    ?>
    <div class="box">
      <div class="request">
        <?php if (isset($authUrl)): ?>
          <a class='login' href='<?php echo $authUrl; ?>'>Connect Me!</a>
        <?php endif; ?>
      </div>

      <?php if (isset($result) && $result): ?>
        <div class="shortened">
          <?php var_dump($result->title); ?>
        </div>
      <?php endif ?>
    </div>
    <?php
    echo pageFooter(__FILE__);
    ?>

我已经解决了这个问题。。。
但是有一个小错误。。。我的api只能获取.jpg文件。它甚至无法获取.jpeg。。。请解决这个问题。。。否则一切正常。。。
这是新的index.php文件

index.php

    <?php
    /*
     * Copyright 2011 Google Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    include_once "templates/base.php";
    session_start();

    set_include_path("src/" . PATH_SEPARATOR . get_include_path());
    require_once 'Google/Client.php';
    require_once 'Google/Http/MediaFileDownload.php';
    require_once 'Google/Service/Drive.php';

    /************************************************
      ATTENTION: Fill in these values! Make sure
      the redirect URI is to this page, e.g:
      http://localhost:8080/fileupload.php
     ************************************************/
    $client_id = 'Your_Client_Id';
    $client_secret = 'Your_Client_Secret';
    $redirect_uri = 'http://localhost/MyApi/Google-Drive';

    $client = new Google_Client();
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->addScope("https://www.googleapis.com/auth/drive");
    $service = new Google_Service_Drive($client);

    if (isset($_REQUEST['logout'])) {
        unset($_SESSION['download_token']);
    }

    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['download_token'] = $client->getAccessToken();
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }

    if (isset($_SESSION['download_token']) && $_SESSION['download_token']) {
        $client->setAccessToken($_SESSION['download_token']);
        if ($client->isAccessTokenExpired()) {
            unset($_SESSION['download_token']);
        }
    } else {
        $authUrl = $client->createAuthUrl();
    }

    /********************************************************
      If we're signed in then lets try to download our file.
     ********************************************************/
    if ($client->getAccessToken()) {
    // This is downloading a file directly, with no metadata associated.
    $file = new Google_Service_Drive_DriveFile();
    $result = $service->files->listFiles(
        $file,
        array('downloadType' => 'media')
    );
}
echo pageHeader("File Download - Downloading a Photo");
if(
        $client_id == '<YOUR_CLIENT_ID>' ||
        $client_secret == '<YOUR_CLIENT_SECRET>' ||
        $redirect_uri == '<YOUR_REDIRECT_URI>'
    ) {
    echo missingClientSecretsWarning();
}
?>
<div>
    <?php if (isset($authUrl)): ?>
    <a class='login' href='<?php echo $authUrl; ?>'>Log In To Your Google Account!</a>
    <?php endif; ?>
</div>
<br>Result:<br><pre>
<?php print_r($result)?>
</pre><br><br>
<?php
    if (isset($result)){
        $i=0;
        echo "Image Path = ".$result['modelData']['items'][$i]['thumbnailLink']."<br>";
        foreach ($result as $key => $value){
            if(strcmp($result['modelData']['items'][$i]['mimeType'],'image/')){
                echo "Entered if cond";
?>
                <div>
                    <img src="<?php echo $result['modelData']['items'][$i]['thumbnailLink'];?>">
                </div>
<?php
            }
            $i++;
        }
    }
?>

    <?php
    /*
     * Copyright 2011 Google Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    include_once "templates/base.php";
    session_start();

    set_include_path("src/" . PATH_SEPARATOR . get_include_path());
    require_once 'Google/Client.php';
    require_once 'Google/Http/MediaFileDownload.php';
    require_once 'Google/Service/Drive.php';

    /************************************************
      ATTENTION: Fill in these values! Make sure
      the redirect URI is to this page, e.g:
      http://localhost:8080/fileupload.php
     ************************************************/
    $client_id = 'Your_Client_Id';
    $client_secret = 'Your_Client_Secret';
    $redirect_uri = 'http://localhost/MyApi/Google-Drive';

    $client = new Google_Client();
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->addScope("https://www.googleapis.com/auth/drive");
    $service = new Google_Service_Drive($client);

    if (isset($_REQUEST['logout'])) {
        unset($_SESSION['download_token']);
    }

    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['download_token'] = $client->getAccessToken();
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }

    if (isset($_SESSION['download_token']) && $_SESSION['download_token']) {
        $client->setAccessToken($_SESSION['download_token']);
        if ($client->isAccessTokenExpired()) {
            unset($_SESSION['download_token']);
        }
    } else {
        $authUrl = $client->createAuthUrl();
    }

    /********************************************************
      If we're signed in then lets try to download our file.
     ********************************************************/
    if ($client->getAccessToken()) {
    // This is downloading a file directly, with no metadata associated.
    $file = new Google_Service_Drive_DriveFile();
    $result = $service->files->listFiles(
        $file,
        array('downloadType' => 'media')
    );
}
echo pageHeader("File Download - Downloading a Photo");
if(
        $client_id == '<YOUR_CLIENT_ID>' ||
        $client_secret == '<YOUR_CLIENT_SECRET>' ||
        $redirect_uri == '<YOUR_REDIRECT_URI>'
    ) {
    echo missingClientSecretsWarning();
}
?>
<div>
    <?php if (isset($authUrl)): ?>
    <a class='login' href='<?php echo $authUrl; ?>'>Log In To Your Google Account!</a>
    <?php endif; ?>
</div>
<br>Result:<br><pre>
<?php print_r($result)?>
</pre><br><br>
<?php
    if (isset($result)){
        $i=0;
        echo "Image Path = ".$result['modelData']['items'][$i]['thumbnailLink']."<br>";
        foreach ($result as $key => $value){
            if(strcmp($result['modelData']['items'][$i]['mimeType'],'image/')){
                echo "Entered if cond";
?>
                <div>
                    <img src="<?php echo $result['modelData']['items'][$i]['thumbnailLink'];?>">
                </div>
<?php
            }
            $i++;
        }
    }
?>