0

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.

Ashish Shah
  • 152
  • 1
  • 3
  • 16
  • 1
    I sure do hope that you have requested API keys, ID's and what not? – Nick Jun 19 '14 at 14:12
  • sorry I am not of any help here. I'd read this https://developers.google.com/drive/web/quickstart/quickstart-php and make sure I have requested the correct API keys (as @Ortix92 suggests). The Google API keys are live as soon as you request them, so there shouldn't be a problem with approvals (unless they changed things in the meanwhile). You go here for that https://code.google.com/apis/console/b/0/?pli=1 (click the start a new project button, switch on "Drive API", click on settings icon, land on other page, click on "Enable" if it says "Drive SDK is not active on this project") – tattvamasi Jun 19 '14 at 14:23
  • @Ortix92 Yes I have requested all the requirements required by the code... And it is working in my Developer account... It is just that it is not working in normal user account... – Ashish Shah Jun 19 '14 at 14:27
  • @tattvamasi it's not a problem at all... you have helped me much more in my previous question... :)... And I have done all the things you have mentioned... Thanks for the suggestion by the way... Please share the information if you find anything regarding this... – Ashish Shah Jun 19 '14 at 14:29
  • @AshishShah after having read your update, I'd go for app review first. It may be that...Wow I had no idea I had to submit Google apps too for review :) – tattvamasi Jun 19 '14 at 15:14
  • @tattvamasi even I didn't know I have to submit my for review to Google... But I am not finding any button or Link to submit my app... Can you tell me how can I submit my app to google for review? Thank for the help – Ashish Shah Jun 20 '14 at 05:58

0 Answers0