Use Google Drive Api to Upload a File Python
Welcome folks I am dorsum with some other blog post. In this post we will exist implementing How to Upload Files to Google Drive using Google Drive REST API V3 from scratch. Google Bulldoze is one of the most popular medium of storing your information in the mod world. For this application nosotros start of all need to create a projection in Google Developers Panel and get your client id and customer secret. This process is illustrated in the next steps in a detailed fashion.
So just create a new projection by providing a certain proper noun or you can select from the created projects.
Afterwards selecting the project just click on create credentials and create a brand new OAuth Client ID.
Just select the pick of web application because we are creating a spider web application which interacts with the Google Drive API.
So in this merely provide localhost for the first field and the 2d field is important it needs to exist the same for your project you can have different redirect url. It is basically the url to which Google redirects you whenever the user grants admission to your application. Select information technology charily it needs to be aforementioned for your projection.
Click on the create button to generate the client id and client underground. Information technology will be dissimilar for you and don't share this with others. Just re-create both and store it somewhere we will exist using it afterward in the application.
After that create your projection and inside your project create a brand new alphabetize.html file which will hold a simple button. For this project we are using XAMMP for local server.
1 2 3 four 5 6 seven 8 9 10 11 12 13 14 fifteen 16 17 18 | < ! DOCTYPE html > < html > < head > < meta charset = "utf-viii" / > < meta http - equiv = "X-UA-Uniform" content = "IE=edge" > < title > Git Login App < / championship > < meta proper noun = "viewport" content = "width=device-width, initial-scale=1" > <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.ane/jquery.min.js" > </script> <script src = "main.js" > </script> < / head > < body > < div > < button id = "login" > Upload Files to Drive < / button > < / div > < / body > < / html > |
After that create a new file main.js which will hold the logic for index.html. In this file we will redirect the user to the permissions folio where users tin can grant access to your application.
1 2 3 4 5 6 7 viii 9 10 xi 12 13 fourteen 15 16 17 18 19 twenty 21 22 23 24 25 26 27 28 29 xxx 31 32 33 34 35 36 37 38 39 twoscore 41 42 43 44 45 46 47 48 49 | $ ( document ) . ready ( part ( ) { // client id of the project var clientId = "" ; // redirect_uri of the project var redirect_uri = "" ; // telescopic of the project var scope = "https://world wide web.googleapis.com/auth/drive" ; // the url to which the user is redirected to var url = "" ; // this is event click listener for the button $ ( "#login" ) . click ( office ( ) { // this is the method which will be invoked it takes 4 parameters signIn ( clientId , redirect_uri , scope , url ) ; } ) ; function signIn ( clientId , redirect_uri , scope , url ) { // the actual url to which the user is redirected to url = "https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=" + redirect_uri + "&prompt=consent&response_type=code&client_id=" + clientId + "&telescopic=" + scope + "&access_type=offline" ; // this line makes the user redirected to the url window . location = url ; } } ) ; |
The whole source lawmaking is total of comments and it is self explanatory we are using jQuery and also just supersede the client id , redirect_uri with your values respectively. When you save this and execute this you will getting this output every bit shown below.
Equally yous tin can meet that whenever you execute this you lot will exist redirected to the screen where you want to select your google account from this list of accounts and after that information technology grants admission to your account by allowing this you will be granting access to this application. If you lot redirect_uri is different from the one you take mentioned in the Google Developers Console. You will getting this fault of redirect_uri.
After that create a brand new file called every bit upload.html which volition be the actual redirect_uri to which the user will state later on allowing access to the application. Just create a new file upload.html and copy paste the code. And also create upload.css and copy paste the code of css.
1 ii 3 4 v six 7 8 9 10 11 12 13 14 15 sixteen 17 18 19 twenty 21 22 23 24 25 | < ! DOCTYPE html > < html > < caput > < meta charset = "utf-viii" / > < meta http - equiv = "Ten-UA-Uniform" content = "IE=edge" > < title > Git Login App < / championship > < meta proper noun = "viewport" content = "width=device-width, initial-scale=one" > < link rel = "stylesheet" type = "text/css" media = "screen" href = "upload.css" / > <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" > </script> <script src = "upload.js" > </script> < / head > < torso > < div > < input id = "files" blazon = "file" name = "files[]" multiple / > < push button id = "upload" > Upload < / push button > < div id = "progress-wrp" > < div form = "progress-bar" > < / div > < div class = "status" > 0 % < / div > < / div > < / div > < div id = "result" > < / div > < / body > < / html > |
i 2 three 4 5 6 vii 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #progress-wrp { border : 1px solid #0099CC; padding : 1px ; position : relative ; height : 30px ; edge - radius : 3px ; margin : 10px ; text - marshal : left ; background : #fff; box - shadow : inset 1px 3px 6px rgba ( 0 , 0 , 0 , 0.12 ) ; } #progress-wrp .progress-bar { meridian : 100 % ; border - radius : 3px ; background - color : #f39ac7; width : 0 ; box - shadow : inset 1px 1px 10px rgba ( 0 , 0 , 0 , 0.xi ) ; } #progress-wrp .condition { top : 3px ; left : l % ; position : absolute ; display : inline - block ; color : #000000; } |
You can see that there is a upload push and also a button from which you tin select files from your computer and upload it to your Google Drive.
Merely create a new upload.js file to create the logic whenever the upload button is clicked from the user. Showtime of all we will substitution the authorization code which is generated in the previous step with the access token. Access token are by and large the medium from which we will making the requests to the API.
1 2 3 iv 5 6 7 8 nine ten 11 12 xiii 14 15 16 17 18 nineteen 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 xl 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 threescore 61 62 63 64 65 66 67 68 69 seventy 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 116 117 118 119 120 121 122 | $ ( document ) . set ( office ( ) { const urlParams = new URLSearchParams ( window . location . search ) ; const lawmaking = urlParams . get ( 'code' ) ; const redirect_uri = "" // replace with your redirect_uri; const client_secret = "" ; // replace with your customer secret const telescopic = "https://www.googleapis.com/auth/drive" ; var access_token = "" ; var client_id = "" // replace it with your client id; $ . ajax ( { blazon : 'POST' , url : "https://world wide web.googleapis.com/oauth2/v4/token" , data : { lawmaking : code , redirect_uri : redirect_uri , client_secret : client_secret , client_id : client_id , scope : scope , grant_type : "authorization_code" } , dataType : "json" , success : part ( resultData ) { localStorage . setItem ( "accessToken" , resultData . access_token ) ; localStorage . setItem ( "refreshToken" , resultData . refreshToken ) ; localStorage . setItem ( "expires_in" , resultData . expires_in ) ; window . history . pushState ( { } , document . title , "/GitLoginApp/" + "upload.html" ) ; } } ) ; function stripQueryStringAndHashFromPath ( url ) { render url . divide ( "?" ) [ 0 ] . separate ( "#" ) [ 0 ] ; } var Upload = function ( file ) { this . file = file ; } ; Upload . epitome . getType = function ( ) { localStorage . setItem ( "type" , this . file . type ) ; return this . file . blazon ; } ; Upload . prototype . getSize = function ( ) { localStorage . setItem ( "size" , this . file . size ) ; return this . file . size ; } ; Upload . epitome . getName = function ( ) { return this . file . proper noun ; } ; Upload . prototype . doUpload = function ( ) { var that = this ; var formData = new FormData ( ) ; // add assoc fundamental values, this will be posts values formData . suspend ( "file" , this . file , this . getName ( ) ) ; formData . append ( "upload_file" , true ) ; $ . ajax ( { type : "Post" , beforeSend : part ( request ) { asking . setRequestHeader ( "Authorization" , "Bearer" + " " + localStorage . getItem ( "accessToken" ) ) ; } , url : "https://www.googleapis.com/upload/drive/v2/files" , data : { uploadType : "media" } , xhr : office ( ) { var myXhr = $ . ajaxSettings . xhr ( ) ; if ( myXhr . upload ) { myXhr . upload . addEventListener ( 'progress' , that . progressHandling , false ) ; } return myXhr ; } , success : part ( data ) { console . log ( data ) ; } , mistake : office ( error ) { panel . log ( error ) ; } , async : true , data : formData , cache : false , contentType : false , processData : false , timeout : 60000 } ) ; } ; Upload . prototype . progressHandling = function ( consequence ) { var per centum = 0 ; var position = event . loaded || outcome . position ; var total = outcome . total ; var progress_bar_id = "#progress-wrp" ; if ( upshot . lengthComputable ) { per centum = Math . ceil ( position / total * 100 ) ; } // update progressbars classes so it fits your lawmaking $ ( progress_bar_id + " .progress-bar" ) . css ( "width" , + percent + "%" ) ; $ ( progress_bar_id + " .status" ) . text ( percent + "%" ) ; } ; $ ( "#upload" ) . on ( "click" , function ( e ) { var file = $ ( "#files" ) [ 0 ] . files [ 0 ] ; var upload = new Upload ( file ) ; // maby bank check size or blazon here with upload.getSize() and upload.getType() // execute upload upload . doUpload ( ) ; } ) ; } ) ; |
You lot can see that we accept successfully uploaded the image to the google drive. My bulldoze shows that i take uploaded the image right now later on pressing the upload push.
Congratulations we are done Uploading files to Google Drive using the Google Drive Remainder API V3 in Javascript from scratch. Thanks for reading this postal service and if you like reading this and wants to read more of this delight subscribe the blog below to become all the notications.
Source: https://codingshiksha.com/how-to-upload-files-to-google-drive-in-javascript-using-google-drive-rest-api-v3
0 Response to "Use Google Drive Api to Upload a File Python"
Post a Comment