added token retrievel and upload

This commit is contained in:
2026-03-27 14:25:44 +01:00
parent e56c7fb7b5
commit b3ad26c580
4 changed files with 91 additions and 0 deletions

7
.gitignore vendored
View File

@@ -1,3 +1,8 @@
# Ignore Secrets in .env WICHTIG
*.env
*.json
# Ignore generated images # Ignore generated images
*.jpg *.jpg
@@ -18,3 +23,5 @@
Thumbs.db Thumbs.db
.DS_Store .DS_Store

30
Get-GDriveToken.ps1 Normal file
View File

@@ -0,0 +1,30 @@
$client = Get-Content ".\client_secret.json" | ConvertFrom-Json
$clientId = $client.installed.client_id
$clientSecret = $client.installed.client_secret
$redirectUri = "http://localhost"
$scope = "https://www.googleapis.com/auth/drive.file"
$authUrl = "https://accounts.google.com/o/oauth2/v2/auth?client_id=$clientId&redirect_uri=$redirectUri&response_type=code&scope=$scope&access_type=offline"
Write-Host "Öffne folgende URL im Browser:"
Write-Host $authUrl
Start-Process $authUrl
$code = Read-Host "Code aus URL eingeben"
$body = @{
code = $code
client_id = $clientId
client_secret = $clientSecret
redirect_uri = $redirectUri
grant_type = "authorization_code"
}
$response = Invoke-RestMethod -Method Post -Uri "https://oauth2.googleapis.com/token" -Body $body
$response | ConvertTo-Json | Out-File "token.json"
Write-Host "Token gespeichert!"

54
Upload-A4ToDrive.ps1 Normal file
View File

@@ -0,0 +1,54 @@
# Token laden
$token = Get-Content ".\token.json" | ConvertFrom-Json
$accessToken = $token.access_token
$folder = "C:\Temp\git\PS-SetEtikettSorte"
# 👉 HIER deine Google Drive Folder ID eintragen
$DriveFolderId = "1kEmjnVL414XYLRfN597K7YKuiFs-FRBv"
$files = Get-ChildItem $folder -Filter "*A4*.jpg"
foreach ($file in $files) {
Write-Host "Upload: $($file.Name)"
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"
# 🔥 HIER angepasst
$metadata = @{
name = $file.Name
parents = @($DriveFolderId)
} | ConvertTo-Json
$fileBytes = [System.IO.File]::ReadAllBytes($file.FullName)
$body = (
"--$boundary$LF" +
"Content-Type: application/json; charset=UTF-8$LF$LF" +
"$metadata$LF" +
"--$boundary$LF" +
"Content-Type: image/jpeg$LF$LF"
)
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($body)
$footer = "$LF--$boundary--$LF"
$footerBytes = [System.Text.Encoding]::UTF8.GetBytes($footer)
$finalBody = New-Object System.IO.MemoryStream
$finalBody.Write($bodyBytes, 0, $bodyBytes.Length)
$finalBody.Write($fileBytes, 0, $fileBytes.Length)
$finalBody.Write($footerBytes, 0, $footerBytes.Length)
$finalBody.Position = 0
Invoke-RestMethod `
-Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" `
-Method Post `
-Headers @{ Authorization = "Bearer $accessToken" } `
-ContentType "multipart/related; boundary=$boundary" `
-Body $finalBody
Write-Host "Fertig: $($file.Name)"
}

0
UploadToGdrive.ps1 Normal file
View File