I made a program to upload images to Google Drive with Python.
Windows 7 Professional SP1 64bit Python 3.6.1 (64bit) pip 9.0.1 google-api-python-client 1.6.2 PyDrive 1.3.1
Install "google-api-python-client" and "PyDrive".
command prompt
>pip install google-api-python-client
>pip install PyDrive
An error occurred during the installation of PyDrive, so I solved it by the following method. http://qiita.com/akabei/items/da70ebf61cc413d5ff0d
You will need your "Client ID" and "Client Secret" to access Google Drive, so get them.
Go to the Google Developers Console and create a project. https://console.developers.google.com/
Enter the project name appropriately.
Create an "OAuth Client ID" from your credentials.
When the message "To create an OAuth client ID, you must first set the service name on the consent screen." Is displayed, set the consent screen.
From the OAuth consent screen, enter the "service name to be displayed to the user" appropriately.
When the creation of client ID is displayed, select "Other", enter an appropriate name, and press Create.
Now you can get the "Client ID" and "Client Secret". Make a note of it so that you will use it later.
Or download the JSON file from your credentials. The JSON file contains the "Client ID" and "Client Secret".
Continue to select the Google Apps API Drive API from the Google Developers Console library.
The following screen will be displayed. Click "Enable" to enable the Google Drive API.
When "Enable" changes to "Disable", the Google Drive API is enabled. (No Google Drive API settings required)
Now you're ready to access Google Drive.
Create a program (imageupload.py) to upload images to Google Drive. This time I'm using CommandLineAuth () because it's called from the command line.
imageupload.py
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)
f = drive.CreateFile({'title': 'test.jpg', 'mimeType': 'image/jpeg'})
f.SetContentFile('test.jpg')
f.Upload()
Create a settings file (settings.yaml) in the same folder as the program. Enter the "Client ID" and "Client Secret" obtained above.
settings.yaml
client_config_backend: settings
client_config:
client_id: <Client ID>
client_secret: <Client secret>
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json
get_refresh_token: True
oauth_scope:
- https://www.googleapis.com/auth/drive.file
- https://www.googleapis.com/auth/drive.install
Prepare the image (test.jpg) used in this test in the same folder as the program.
Run the program from the command prompt. At the first execution, you will be prompted to enter the verification code, so display the displayed URL in your browser.
command prompt
>python imageupload.py
C:\Python36\lib\site-packages\oauth2client\_helpers.py:255: UserWarning: Cannot access credentials.json: No such file o
r directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/auth?client_id=915107419881-unme2nu12t3d5sf10cmpudgo20jq3mtg.apps.googleuserco
ntent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file+h
ttps%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.install&access_type=offline&response_type=code&approval_prompt=force
Enter verification code:
The browser will display a screen to approve access to Google Drive, so "Allow" it.
The identification code will be displayed in the browser, so copy it and paste it into the command prompt.
After pasting the identification code, enter the enter key.
command prompt
Enter verification code: 4/aiZHOmoP3Ufz5Y-d8BvTFq8-IL7Do2GK7FOAAurccN4
Authentication successful.
If it finishes normally, the image will be uploaded to Google Drive. (You do not need to enter the identification code from the second time onward.)
Google drive https://drive.google.com/drive/
"Folder ID" is required when uploading to a folder in Google Drive. Open the folder you want to upload in your browser and the last part of the displayed URL is the folder ID.
You can upload to that folder by specifying the folder ID in the parameter of CreateFile ().
imageupload.py
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)
folder_id = '0B5fATn-vXv5wZ0RlM1JpQzdFTk0'
f = drive.CreateFile({'title': 'test.jpg',
'mimeType': 'image/jpeg',
'parents': [{'kind': 'drive#fileLink', 'id':folder_id}]})
f.SetContentFile('test.jpg')
f.Upload()
I referred to the following site.
Using Google Drive with PyDrive http://python.keicode.com/advanced/pydrive.php
Recommended Posts