Last active
September 16, 2019 09:44
-
-
Save niccolomineo/3a2c0a1e5dde6785a657cb3eb8ad1c3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import mimetypes | |
import jwt | |
import google.auth.crypt | |
import google.auth.jwt | |
import requests | |
credentials_file = json.load(open( | |
os.path.dirname(os.path.realpath(__file__)) + | |
"/my-credentials.json" | |
)) | |
now = int(time.time()) | |
payload = { | |
"iss": credentials_file["client_email"], | |
"sub": credentials_file["client_email"], | |
"aud": "https://www.googleapis.com/oauth2/v4/token", | |
"iat": now, | |
"exp": now + 3600, | |
"scope": "https://www.googleapis.com/auth/drive", | |
} | |
additional_headers = { | |
"kid": credentials_file["private_key_id"], | |
"alg": "RS256", | |
"typ": "JWT" | |
} | |
signed_jwt = jwt.encode( | |
payload, | |
credentials_file["private_key"], | |
headers=additional_headers, | |
algorithm="RS256" | |
) | |
data = { | |
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer", | |
"assertion": signed_jwt.decode("UTF-8") | |
} | |
request = requests.post( | |
url="https://www.googleapis.com/oauth2/v4/token", data=data) | |
if request.ok: | |
access_token = request.json() | |
headers = { | |
"Authorization": access_token["token_type"] + " " + access_token["access_token"], | |
} | |
metadata = { | |
"name": img_path.split("/")[-1], | |
"parents": ["<GOOGLE-DRIVE-FOLDER-ID>"] | |
} | |
files = { | |
"data": ("metadata", json.dumps(metadata), "application/json; charset=UTF-8"), | |
"file": (mimetypes.guess_type(img_path)[0], open(img_path, "rb")) | |
} | |
request = requests.post( | |
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", | |
headers=headers, | |
files=files | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import mimetypes | |
from google.oauth2 import service_account | |
from googleapiclient.discovery import build | |
from googleapiclient.http import MediaFileUpload | |
credentials_file = os.path.dirname(os.path.realpath(__file__)) + "/mycredentials.json" | |
credentials = service_account.Credentials.from_service_account_file( | |
credentials_file) | |
scoped_credentials = credentials.with_scopes( | |
["https://www.googleapis.com/auth/drive"]) | |
service = build("drive", "v3", credentials=scoped_credentials) | |
body = { | |
"name": img_path.split("/")[-1], | |
"parents": ["<GOOGLE-DRIVE-FOLDER-ID>"] | |
} | |
media_body = MediaFileUpload( | |
img_path, | |
mimetypes.guess_type(img_path)[0], | |
) | |
file = service.files().create(body=body, media_body=media_body).execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment