Google Api is not working properly

血红的双手。 提交于 2019-12-25 02:53:09

问题


I am using google api (php). But it is not working as I want it to. I am using this api to import user's photos to my website. But I am not able to import them. Infact I can Import the images from the user account in which I have created the app. But from any other accounts I am not able to import images. My Index file is as below:

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>}
?>

Can anyone tell me what is the problem?

FYI: I have not submitted my app for the review yet.

Thank you,

Update: There is one update I found. In one user account (In which I have created app) is is asking for user's authorization for public profile as well as for photos. But in another user account it is asking for the user's authorization for only user's public profile.

来源:https://stackoverflow.com/questions/24308664/google-api-is-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!