feat(google-drive): new Google Drive integration
Drive API v3 (25 commands): shared drive management, change tracking, Drive activity queries, file search/get/create/upload/download/copy/ move/delete, permission list/create/update/delete, and Drive labels. Service-account auth with domain-wide delegation (JWT RS256), runs on a remote engine (requires PyJWT + cryptography). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import json, os, sys, time, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
import jwt
|
||||
|
||||
import base64, mimetypes
|
||||
|
||||
TOKEN_URL = "https://oauth2.googleapis.com/token"
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _token(scopes, subject=None):
|
||||
cfg = _cfg()
|
||||
raw = cfg.get("service_account_json", "")
|
||||
sa = json.loads(raw) if isinstance(raw, str) else raw
|
||||
if not sa.get("client_email") or not sa.get("private_key"):
|
||||
raise Exception("service_account_json must contain client_email and private_key")
|
||||
now = int(time.time())
|
||||
aud = sa.get("token_uri") or TOKEN_URL
|
||||
payload = {
|
||||
"iss": sa["client_email"],
|
||||
"scope": " ".join(scopes),
|
||||
"aud": aud,
|
||||
"iat": now,
|
||||
"exp": now + 3600,
|
||||
}
|
||||
sub = subject or cfg.get("user_id") or ""
|
||||
if sub:
|
||||
payload["sub"] = sub
|
||||
assertion = jwt.encode(payload, sa["private_key"], algorithm="RS256")
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
||||
"assertion": assertion,
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(aud, data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("Token request failed: " + json.dumps(tok))
|
||||
return tok["access_token"]
|
||||
|
||||
|
||||
def request(method, url, scopes, params=None, body=None, subject=None):
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"Accept": "application/json", "Authorization": "Bearer " + _token(scopes, subject)}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
SCOPES = ["https://www.googleapis.com/auth/drive"]
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
file_id = inputs.get("file_id")
|
||||
content_base64 = inputs.get("content_base64")
|
||||
if not file_id:
|
||||
raise Exception("file_id is required")
|
||||
if not content_base64:
|
||||
raise Exception("content_base64 is required")
|
||||
subject = inputs.get("user_id")
|
||||
|
||||
content = base64.b64decode(content_base64)
|
||||
content_type = mimetypes.guess_type(inputs.get("file_name") or "")[0] or "application/octet-stream"
|
||||
|
||||
url = ("https://www.googleapis.com/upload/drive/v3/files/"
|
||||
+ urllib.parse.quote(file_id, safe="")
|
||||
+ "?uploadType=media&supportsAllDrives=true&fields=*")
|
||||
headers = {
|
||||
"Accept": "application/json",
|
||||
"Authorization": "Bearer " + _token(SCOPES, subject),
|
||||
"Content-Type": content_type,
|
||||
}
|
||||
req = urllib.request.Request(url, data=content, headers=headers, method="PATCH")
|
||||
with urllib.request.urlopen(req, timeout=120) as r:
|
||||
print(json.dumps(json.loads(r.read())))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user