-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to authenticate without LocalWebserverAuth() ? #230
Comments
Hey, yes service account - see example here #21 or https://docs.iterative.ai/PyDrive2/pydrive2/#pydrive2.auth.GoogleAuth.LocalWebserverAuth . Please give one of those a try and let us know if something doesn't work. Also, see how |
Thanks you for your help, the example you provided is working. One last question i have a function to upload files like this def upload_file(filepath, filename, id_drive_folder=DRIVE_FOLDER):
"""
upload a file to desired folder.
:param filepath: ubicacion del archivo
:param filename: nombre del archivo
:param id_drive_folder: id de la carpeta del drive
:return:
"""
gdrive = login()
metadata = {
'parents': [
{"kind": "drive#fileLink", "id": id_drive_folder}
],
'title': f'{filename}'
}
# create file
archivo = gdrive.CreateFile(metadata=metadata)
# set the content of the file
archivo.SetContentFile(filepath)
# upload the file to google drive
archivo.Upload() this creates a new instance of GoogleDrive() for every call when doing |
@shcheklein im trying to upload a base64 image stored in memory, but SetContentFile doesnt accepts io.BytesIO(). Exists a way to uploads files with io.BytesIO? |
One instance should be enough. You don't need to do the whole workflow every time (it'll be cached, but anyway you don't need this) |
yes, you could do file.content = io.BytesIO(...)
files.Upload() |
thanks for your help. I couldn't make it work with
# create file
archivo = gdrive.CreateFile(metadata=metadata)
# set the content of the file
archivo.content = bytes_file
# upload the file to google drive
archivo.Upload() then to execute import base64
import io
file = open(r'C:\Users\xxx\Downloads\base64.txt', 'r')
# read base64 string
base64_string: str = file.read()
# decode to data bytes
image_bytes: bytes = base64.b64decode(base64_string)
# Buffered I/O implementation using an in-memory bytes buffer.
image_file = io.BytesIO(image_bytes)
# upload file
# file, filename, mimetype
upload_media(image_bytes, 'test2_4', 'image/jpeg') |
@shcheklein if you want i can contribute to the docs with how to authenticate with a Service Account and how to upload media. |
@matiasrebori that would very helpful, thanks.
I'm not sure about this tbh. It's quite explicit and does one thing. |
Ok, I'm not advanced on GitHub, should I open a new issue to link later with a PR? |
Creating a PR if fine an enough! |
Hi, do you know if the low level API archivo = gdrive.ListFile(query).GetList()
query = {'q': f"title = '{filename}' and mimeType='{mimetype}'"} the |
I don't think My point was that, even if you do multiple instances it should be also fine if you use some default settings since PyDrive will create a files with credentials that it'll try to reuse next time. |
Hi, is there a way to authenticate to Drive without using the LocalWebserverAuth() method, because it promps a login page from the browser and i dont want this behaviour.
I have a flask application and one feature is to save images to a specific google drive folder (uses a single organization account), this runs in a linux vm.
i have a
client_secrets.json
file, this is the JSON downloaded from Google Developers Console, also i set thesettings.yaml
file to create acredentials.json
file to automate the consequents authentications.The problem is i dont want to manually login for the first time if there isnt a
credentials.json
file.Im using the next piece of code to login to google drive
I have a code to get data from Google Sheets and only uses the JSON file from Google Developers Console, in this code i named this file
credentials.json
. So i think theres probably a similiar way to authenticate to Google Drive.The text was updated successfully, but these errors were encountered: