

- #Google drive api upload file to folder python how to#
- #Google drive api upload file to folder python install#
- #Google drive api upload file to folder python code#
#Google drive api upload file to folder python code#
Awesome job! We have now obtained our access token which we can use to connect to Google Drive API resources. Go to the website and retrieve the access token Writing Python Code Now make a simple app.py file and write the following python code which is show below.It will actually takes this access token and upload the file to google drive using the google drive api v3. Then click on AllowĪdditionally, you will see a new file credentials.json gets created in the credentials folder. Select the Google account on which you set up your API and credentials in the previous step. For the first time only, you need to execute the script from your terminalĪ browser tab will open after the above command execution. Note: if you execute the above script from an IDE (I use P圜harm), you would get an error. Follow the below commands to get your access token. Now, save the file and go to your terminal. Your access token will be stored in credentials/credentials.json upon the browser authentication, which I will go through after this. The connect_to_google_drive.py is the Python file that contains instructions to connect to Google Drive API. Your project structure should look like this, to begin with.
#Google drive api upload file to folder python how to#
If you don’t know how to do this, follow this tutorial here. Your access token will be stored in credentials/credentials.json upon the browser.
#Google drive api upload file to folder python install#
# install all the dependent packages pip install google-api-python-client oauth2clientĬreate a folder in the project directory called credentials to store your client_secret.json which you can download from your Google Console. The connecttogoogledrive.py is the Python file that contains instructions to connect to Google Drive API. $ virtualenv venv_google_api $ source venv_google_api/bin/activate In your terminal run the following to create and use a virtual environment. We are going to start with installing google-API-python-client and oauth2client libraries. Since the API resources that we want to access contain sensitive scope (accessing files in Google Drive), we need to do a browser authentication for the first time.īefore we begin, let’s set up a virtual environment to contain and install all the dependencies for this project. To be able to access the API resources, we need to get the access token. When I first started working with the API, I was having difficulties, and only after using it a few times that I developed a better understanding. Web authentication is required again if SCOPES changed.Ĭreative Commons Attribution-NonCommercial 4.0 International License.A lot of companies are using Google Drive to manage and store some of their files so that their employees can access them easily from anywhere as long as there’s internet access. NOTE: Web authentication is only required the first time, after which the authorization should be stored in TOKEN_FILE. NOTE: The script only check filename, and doesn’t reupload file when content changed but filename doesn’t change.

Service = build( 'drive', 'v3', http =creds. flow_from_clientsecrets(CREDENTIAL_FILE, SCOPES) TOKEN_FILE = 'gdrive_sync_token.json' def sync_folder(local_folder, gdrive_folder_name):įlow = client. To transfer ownership of a file in 'My Drive', do one of the following: Create a file permission granting a specific user ( typeuser) owner access ( roleowner ). Import os import glob from googleapiclient.discovery import buildįrom googleapiclient.http import MediaFileUploadįrom oauth2client import file as oauth2file, client, toolsĬREDENTIAL_FILE = 'gdrive_sync_credential.json'
