J Antoni

J Antoni

  • NA
  • 81
  • 35.8k

GDrive Upload not happening when moved to Staging url

Sep 30 2016 6:38 AM
I am using google-drive-sdk to upload a pdf file (dynamically generated in the php web app) to Gdrive.

About my app

On click of submit button the users are redirected to a THANKYOU page. At the background

pdf file gets generated
and this file will be uploaded to Gdrive account --(configured the client id & secret key for the app in Google Console) :
I am able to upload to the configured GDrive but first time to authenticate I run the url(redirect url given in Google Console) in browser say

http://localhost:3422/wordpress/wp-content/plugins/mktProc/google-api-php-client/gDrive_access/pdf_results_upload.php

This takes me to GDrve Request For Permission page and prompts Allow\Deny Access and works perfectly in local development environment

My Problem
But for moving the code to Staging server, in Google Developer Console I change the origin & return uri to Staging uri like the below.

http://hyperAd.com/wp-content/plugins/mktProc/google-api-php-client/gDrive_access/pdf_results_upload.php

After this when I execute the app, it does not go to the Google Request for Permission page to prompt Allow\Deny. So no access token is received to proceed insert file to GDrive.

What is that I am missing here or what mistake have I done here?

What I have tried
I have tried changing http to https
it doesn't allow
Is the return url at staging correct?
The return url in my dev Environment is correct and its working perfectly

-----

My current code



if(!session_id()){session_start();} 
  require_once realpath(dirname(__FILE__) . '/../autoload.php');   
   $credentialsPath = realpath(dirname(__FILE__) . '/../../../../../'); 
  $pdf_filename=''; try {   if(isset($_SESSION['pdf_filename']) && $_SESSION['pdf_filename']) 
       $pdf_filename=$_SESSION['pdf_filename'];  } 
 catch(Exception $ex) 
 {       echo ("This is for GDrive Upload from MiniROI");  } 
$pfdStoragePath=dirname(__FILE__)."/../../vendor/pdf/".$pdf_filename; 
/************************************************   ATTENTION: Fill in these values! Make sure   the redirect URI is to this page, e.g:   http://localhost:8080/fileupload.php  ************************************************/   
$client_id = '374094698471-5.apps.googleusercontent.com';
 $client_secret = 'd374094698471y';
 $redirect_uri = 'http://localhost:3422/wordpress/wp-content/plugins/miniROIMarketo/google-api-php-client/gDrive_access/roi_results_upload.php';     
 $client = new Google_Client(); 
$client->setClientId($client_id); 
$client->setClientSecret($client_secret); 
$client->setRedirectUri($redirect_uri);
 $client->addScope("https://www.googleapis.com/auth/drive");
 $client->setAccessType("offline"); //$client->setApprovalPrompt('force'); 
 $service = new Google_Service_Drive($client); 
  if (isset($_REQUEST['logout'])) { 
  unset($_SESSION['upload_token']); } 
$accessToken="";  //log  $plugin_dir ='../../vendor/pdf/log.txt';
    $accessFilePath=$credentialsPath.'\miniROIAccessToken.json'; 
  if (file_exists($accessFilePath)) {  
   $accessToken = json_decode(file_get_contents($accessFilePath), true);        }  
  elseif (isset($_GET['code'])) {
   $client->authenticate($_GET['code']);  
    $accessToken  = $client->getAccessToken();   
   file_put_contents($credentialsPath.'\miniROIAccessToken.json', json_encode($accessToken)); 
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];  
 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } 
  if ($accessToken) {   $client->setAccessToken($accessToken); 
  if ($client->isAccessTokenExpired()) {     unset($accessToken);   } } 
else {   $authUrl = $client->createAuthUrl(); }   
/************************************************   If we're signed in then lets try to upload our   file.   ************************************************/ 
try {    if ($client->getAccessToken()) {  
          if($pdf_filename!=="")   {           
 $file = new Google_Service_Drive_DriveFile();   
$file->setTitle($pdf_filename);   
$file->setDescription('Document');  
 $file->setMimeType('application/pdf');   
$result = $service->files->insert(       $file,       array(         'data' => file_get_contents($pfdStoragePath),         'mimeType' => 'application/pdf',         'uploadType' => 'multipart'       )   );   
  $pdf_filename="";   unset($_SESSION['pdf_filename']);   }       } } 
catch (Exception $ex) {     echo "Unexpected issue encountered. Please Try again later."; } 
     ?>

 
 
 

Attachment: gDrive_upload.zip

Answers (1)