File pick from Google Drive.
- Demo LInk: http://www.weanswer.xyz/demo/googleDrive/
Follow the steps
- Download Code
- How to download the file from google drive selected
- Create account https://console.developers.google.com/
- Create project
- Library->Search google drive and enable API
- Library->Search google picker and enable API
- Create a credential for developerKey ->API Key -> Browser key and Get API key
- Create credential clientId->Oauthclient->Web application->Get clientId
- if your demo from localhost then Authorised JavaScript origins = “http://localhost” and Authorized redirect URIs =”http://localhost/googleDrive/”
Above API Key and Client id replace in below code
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 |
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Google Picker Example</title> <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> <script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> <script type="text/javascript"> // The Browser API key obtained from the Google Developers Console. var developerKey = 'AIzaSyB7yfM3nXnT5_7s5X2XvO5Ef790ibbkqC1'; // The Client ID obtained from the Google Developers Console. Replace with your own Client ID. var clientId = "1052577024753-tj3c1k40d6crnqcf6le89kbcm2j7b414.apps.googleusercontent.com"; // Scope to use to access user's photos. var scope = ['https://www.googleapis.com/auth/drive.file']; var pickerApiLoaded = false; var oauthToken; // Use the API Loader script to load google.picker and gapi.auth. function onApiLoad() { gapi.load('auth', {'callback': onAuthApiLoad}); gapi.load('picker', {'callback': onPickerApiLoad}); } function onAuthApiLoad() { window.gapi.auth.authorize( { 'client_id': clientId, 'scope': scope, 'immediate': false }, handleAuthResult); } function onPickerApiLoad() { pickerApiLoaded = true; createPicker(); } function handleAuthResult(authResult) { if (authResult && !authResult.error) { oauthToken = authResult.access_token; createPicker(); } } // Create and render a Picker object for picking user Photos. function createPicker() { if (pickerApiLoaded && oauthToken) { var picker = new google.picker.PickerBuilder(). addViewGroup( new google.picker.ViewGroup(google.picker.ViewId.DOCS). addView(google.picker.ViewId.DOCUMENTS). addView(google.picker.ViewId.PRESENTATIONS)). setOAuthToken(oauthToken). setDeveloperKey(developerKey). setCallback(pickerCallback). build(); picker.setVisible(true); } } // A simple callback implementation. function pickerCallback(data) { var url = 'nothing'; var name = 'nothing'; if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) { var doc = data[google.picker.Response.DOCUMENTS][0]; url = doc[google.picker.Document.URL]; name = doc.name; var param = {'fileId': doc.id, 'oAuthToken': oauthToken, 'name': name} console.log(param); document.getElementById('result').innerHTML = "Downloading..."; $.post('download.php', param, function (returnedData) { document.getElementById('result').innerHTML = "Download completed"; }); // var val = name.toLowerCase(); // var regex = new RegExp("(.*?)\.(js|js)$"); // if (!(regex.test(val))) { // alert('Please select correct file format'); // } } // var message = 'You picked: ' + url; // document.getElementById('result').innerHTML = message; } </script> </head> <body> <button onclick="onApiLoad()">Pick From Google Drive</button> <div id="result"></div> <!-- The Google API Loader script. --> <script type="text/javascript" src="https://apis.google.com/js/api.js"></script> </body> </html> |
- File download code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php $param = $_REQUEST; $oAuthToken = $param['oAuthToken']; $fileId = $param['fileId']; $getUrl = 'https://www.googleapis.com/drive/v2/files/' . $fileId . '?alt=media'; $authHeader = 'Authorization: Bearer ' . $oAuthToken; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $getUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, array($authHeader)); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); file_put_contents($param['name'], $data); echo json_encode($error); ?> |
where’s the file download??
Added – http://www.weanswer.xyz/wp-content/uploads/2016/08/googleDrive.zip