Google Api无法正常运行

时间:2014-06-19 14:09:03

标签: php google-api-php-client google-api-client

我正在使用google api(php)。但它并不像我想要的那样工作。我正在使用此API将用户的照片导入我的网站。但我无法导入它们。事实上,我可以从我创建应用程序的用户帐户导入图像。但是从任何其他帐户我无法导入图像。我的索引文件如下:

的index.php

<?php
/************************************************************************
 * Plugin Name: Google Drive Plugin
 * Plugin URI: http://www.picpixa.com/
 * Version: Current Version
 * Author: Ashish Shah
 * Description: Plugin To Import Images From User's Google Drive Account
 ***********************************************************************/

/*
 * 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";
ini_set("display_errors",0);
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_APP_SECRET';
$redirect_uri = 'REDIRECT_URI';

$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')
    );
}
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>

<?php
    if (isset($result)){
        $i=0;
        $temp=0;
        /*echo "Result<pre>";
        print_r($result);
        echo "</pre>";*/
        $showBtn=False;
        echo "<form method='post' action='index.php'><table> <tr>";
        foreach ($result as $key => $value){            
            if(strcmp($result['modelData']['items'][$i]['mimeType'],'image/jpeg') == 0
                || strcmp($result['modelData']['items'][$i]['mimeType'],'image/jpg') == 0
                || strcmp($result['modelData']['items'][$i]['mimeType'],'image/png') == 0)
            {
                if($temp==0 || $temp%5 ==0)
                {
                    echo "</tr><tr>";
                }
                if(isset($result['modelData']['items'][$i]['thumbnailLink'])){
                    //echo "Alternate Link: ".$result['modelData']['items'][$i]['alternateLink'];die;
?>
                    <td align="center">
                        <input type="checkbox" id="gDrive_<?=$temp;?>" name="gDrive[]"  value="<?php echo $result['modelData']['items'][$i]['thumbnailLink'];?>">
                        <input type="hidden"  id="thumbGDrive_<?=$temp;?>" name="thumbGDrive[]"  value="<?php echo $result['modelData']['items'][$i]['alternateLink'];?>">
                    </td>
                    <td><img src="<?php echo $result['modelData']['items'][$i]['thumbnailLink'];?>"></td>
            <?php
                }
                $temp++;
            }
            $i++;
            $showBtn=True;
        }
        if($showBtn){
            echo "<tr><td colspan='2'><input type='submit' name='copy' value='Copy Selected Files' ></td></tr>";
        }
        echo "</tr><table></form>";
    }
?>
<?php 
    if(isset($_POST['copy']))
    {<My code after importing images>}
?>

谁能告诉我这是什么问题?

仅供参考:我还没有提交我的应用程序进行审核。

谢谢,

更新:我找到了一个更新。在一个用户帐户(我在其中创建了应用程序)中,要求用户对公共个人资料以及照片进行授权。但在另一个用户帐户中,它仅针对用户的公开个人资料要求用户授权。

0 个答案:

没有答案