Hello everyone!
I have a code that works for me in vs code, but the request and json doesn't work directly in ignition:
#that code works for me on vs code
import json
import requests
headers = {
"Authorization":"Bearer xxxx-xxxxx"
}
para = {
"name":"ejemplo3.jpg",
"parents":["xxxxxx"]
}
files = {
'data':('metadata',json.dumps(para),'application/json;charset=UTF-8'),
'file':open('Screenshot_2.png','rb')
}
r = requests.post("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
I am looking for a way to select a file with file explorer, and upload to google drive, that is why I have the first part of the code where I select the file and get the name:
import os
#paso 1
selectedFile = event.source.parent.getComponent('file_explorer').selectedPath
print "archivo seleccionado:"
print selectedFile
flag_src_path_IsFile = event.source.parent.getComponent('file_explorer').selectedPathIsFile
print "flag_src_path_IsFile:"
print flag_src_path_IsFile
nameFile = os.path.basename(selectedFile)
print "nombre el archivo seleccionado en file explorer:"
print nameFile
and the step 2 that I have to use the google playground api is this, although it doesn't work for me
it could be something like this:
import os
import urllib2
import json
# Token de acceso generado en Google Cloud Console
access_token = "xxxxxxxxxxxxxx"
# URL de la API de Google Drive para subir archivos
url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
# Encabezados de la solicitud, incluyendo el token de acceso
headers = {
"Authorization": "Bearer " + access_token,
"Content-Type": "multipart/related; boundary=foo"
}
# Metadatos del archivo, incluyendo el nombre y la ubicaciĂłn
metadata = {
"name": "energia-solar.jpg",
"parents": ["xxxxxx"]
}
# Contenido del archivo, codificado en base64
file_path = r"C:\Users\BJ_IT\Downloads\aimage.png"
with open(file_path, "rb") as f:
file_content = f.read()
file_content_base64 = file_content.encode("base64").replace("\n", "")
# Cuerpo de la solicitud
body = (
"--foo\r\n" +
"Content-Type: application/json; charset=UTF-8\r\n\r\n" +
json.dumps(metadata) + "\r\n" +
"--foo\r\n" +
"Content-Type: image/jpeg\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"Content-Disposition: attachment; filename=\"energia-solar.jpg\"\r\n\r\n" +
file_content_base64 + "\r\n" +
"--foo--"
)
# Crear y enviar la solicitud
request = urllib2.Request(url, data=body, headers=headers)
response = urllib2.urlopen(request,timeout=10)
# Leer la respuesta y obtener el ID del archivo subido
response_data = json.loads(response.read())
file_id = response_data["id"]
print("Archivo subido con éxito. ID del archivo: " + file_id)
could i get help? I have read:
I appreciate your answers...