无需用户干预即可将文件上传到GDrive

时间:2016-08-26 00:45:20

标签: google-drive-api google-oauth

我正在使用google-drive-sdk将pdf文件(在php web app中动态生成)上传到Gdrive。

关于我的应用

点击提交按钮,用户会在页面上看到一个THANKYOU。在后台

  1. 生成pdf文件

  2. 此文件需要上传到Gdrive帐户 - (在Google控制台中配置应用的客户端ID和密钥) :

  3. 我可以上传到配置好的GDrive,但第一次验证我在浏览器中运行网址(在Google控制台中指定的重定向网址)(*说

      

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

    *) 这会将我带到GDrve页面并提示Allow \ Deny Access。

    我点击“允许”,然后就可以上传到Grdive了。

    但是我希望在没有提示Allow \ Deny让用户进行交互的情况下发生这种情况。

    请详细说明我需要添加的内容,以便当用户点击“提交”按钮时,生成的文件会自动插入到GDrive中。

    我在网上看了很多但是无法理解并让它发挥作用。 请帮我处理包含的步骤和代码。 我正在使用的代码

                    <?php
    
                    include_once "templates/base.php";
                    if(!session_id()){session_start();}
    
                    require_once realpath(dirname(__FILE__) . '/../autoload.php');
    
                    $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 CalWeb");
                     }
                    $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 = 'xxxx.apps.googleusercontent.com';
                    $client_secret = 'xxxxSB0tNVy';
                    $redirect_uri = 'http://localhost:3422/wordpress/wp-content/plugins/mktProc/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']);
                    }
    
                    if (isset($_GET['code'])) {
                      $client->authenticate($_GET['code']);
                      $_SESSION['upload_token'] = $client->getAccessToken();
                      $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
                      header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
                    }
    
                    if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
                      $client->setAccessToken($_SESSION['upload_token']);
                      if ($client->isAccessTokenExpired()) {
                        unset($_SESSION['upload_token']);
                      }
                    } 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'
                          )
                      );
                      //echo $result->webContentLink;
                      $pdf_filename="";
                      unset($_SESSION['pdf_filename']);
                      }
    
                      //if (isset($result) && $result) $_SESSION['webContentLink']=$result->webContentLink;
                      //echo $result->webContentLink;
                    }//if ($client->getAccessToken()) {
                    } catch (Exception $ex) {
                        echo '';
                    }
    
                    ?>
    
                    <div class="box">
                      <div class="request">
                        <?php if (isset($authUrl)): ?>
                          <a class='login' href='<?php echo $authUrl; ?>'>Connect Me!</a>
                         <?php else: ?>  <?php echo "Access Allowed" ?>
                        <?php endif; ?>
                      </div>
    
                      <?php if (isset($result) && $result): ?>
                        <div class="shortened">
                             <?php echo "aaa"; ?>
                          <?php echo $result->webContentLink; ?>
                         <?php echo "bbb"; ?>
    
                        </div>
                      <?php endif ?>
    
                        <?php if($pdf_filename==="") ?>
                        <?php echo "This is for GDrive Upload from MiniROI" ?>
                    </div>
    

0 个答案:

没有答案