Create Auth URL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$clientId = ""; $clientSecret = ""; $redirectUri = ""; $client = new \Google_Client(); $client->setClientId($clientId); $client->setClientSecret($clientSecret); //$client->setAuthConfig('client_secret.json'); $client->addScope("https://www.googleapis.com/auth/drive.file"); $client->setRedirectUri($redirectUri); // offline access will give you both an access and refresh token so that // your app can refresh the access token without user interaction. $client->setAccessType('offline'); // Using "consent" ensures that your application always receives a refresh token. // If you are not using offline access, you can omit this. //$client->setApprovalPrompt("select_account"); //$client->setIncludeGrantedScopes(true); // incremental auth $client->setPrompt('consent'); return $client->createAuthUrl(); |
Create auth Token
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$clientId = ""; $clientSecret = ""; $redirectUri = ""; $code = $request->code; $client = new \Google_Client(); $client->setClientId($clientId); $client->setClientSecret($clientSecret); //$client->setAuthConfig('client_secret.json'); $client->addScope("https://www.googleapis.com/auth/drive.file"); $client->setRedirectUri($redirectUri); // offline access will give you both an access and refresh token so that // your app can refresh the access token without user interaction. $client->setAccessType('offline'); // Using "consent" ensures that your application always receives a refresh token. // If you are not using offline access, you can omit this. //$client->setApprovalPrompt("select_account"); //$client->setIncludeGrantedScopes(true); // incremental auth $client->authenticate($code); $access_token = $client->getAccessToken(); $service = new \Google_Service_Drive($client); $unique_id = $service->about->get(array('fields' => 'user'))->getUser()->getemailAddress(); $auth_response = json_encode($access_token); // store this variable data into your database for accessing new token and gdrive data $auth_response |
Upload File in Google Drive
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
$oldTokenData = ""; $clientId = ""; $clientSecret = ""; $redirectUri = ""; $folderPath = ""; $fileName = ""; $localFilePath = ""; $fileContent = file_get_contents($localFilePath); $auth_response_array = json_decode($oldTokenData); if(isset($auth_response_array->refresh_token)){ $client = new \Google_Client(); $client->setClientId($clientId); $client->setClientSecret($clientSecret); $client->addScope("https://www.googleapis.com/auth/drive.file"); $client->setRedirectUri($redirectUri); // offline access will give you both an access and refresh token so that // your app can refresh the access token without user interaction. $client->setAccessType('offline'); $newtokendata = $client->refreshToken($auth_response_array->refresh_token); if(isset($newtokendata['refresh_token'])){ // update new token into your database $mime_type = mime_content_type($localFilePath); $file = file_get_contents($localFilePath); $fileSize = strlen($file); if($fileSize==0){ return false; } $folder_names = explode("/",$folderPath); $client->setDefer(false); $i=0; $service = new \Google_Service_Drive($client); foreach($folder_names as $folder){ if($folder!=""){ if(isset($new_folder->id)){ $parent = array($new_folder->id); $query = array('q'=>'name="'.$folder.'" '."and mimeType='application/vnd.google-apps.folder' and trashed=false and parents='".$new_folder->id."'"); }else{ $parent = array(); $query = array('q'=>'name="'.$folder.'" '."and mimeType='application/vnd.google-apps.folder' and trashed=false"); } $drive_folders = $service->files->listFiles($query); if($drive_folders){ if(count($drive_folders)>0){ foreach($drive_folders as $drive_folder){ $new_folder = $drive_folder; } }else{ $fileMetadata = new \Google_Service_Drive_DriveFile(array( 'name' => $folder, 'parents' => $parent, 'mimeType' => 'application/vnd.google-apps.folder')); $new_folder = $service->files->create($fileMetadata, array('fields' => 'id')); } } } $i++; } if(isset($new_folder->id)){ $parent = array($new_folder->id); }else{ $parent = array(); } $fileMetadata = new \Google_Service_Drive_DriveFile(array('name' => $fileName,'parents' => $parent)); if($fileSize > 4194304){ $chunkSizeBytes = 1 * 1024 * 1024; $client->setDefer(true); $request = $service->files->create($fileMetadata); $media = new Google_Http_MediaFileUpload( $client, $request, $mime_type, null, true, $chunkSizeBytes ); $media->setFileSize(filesize($localFilePath)); $status = false; $handle = fopen($localFilePath, "rb"); while (!$status && !feof($handle)) { // read until you get $chunkSizeBytes from TESTFILE // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file // An example of a read buffered file is when reading from a URL $chunk = readFileChunk($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } // The final value of $status will be the data from the API for the object // that has been uploaded. $result = false; if ($status != false) { $result = $status; } fclose($handle); return $result; }else{ $file = $service->files->create($fileMetadata, array( 'data' => $fileContent, 'mimeType' => 'application/octet-stream', 'uploadType' => 'multipart', 'fields' => 'id')); return $file; } } } |
Leave a reply