I have this example where I upload a csv file in my Google Drive, but a browser page is opened, and I don't want it. In addition to this, I created a project in Google Cloud where I enabled Google Drive API, generated a OAuth 2.0 client ID and added the client_screts.json file in my working directory.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file1 = drive.CreateFile({"mimeType": "text/csv"})
file1.SetContentFile("test.csv")
file1.Upload()
print("finished")
In my new implementation, I tried creating a new project where I did the same, but with a service account. I tried to follow the example here, but with no results: How to connect pydrive with an Service Account
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials
gauth = GoogleAuth()
scope = ["https://www.googleapis.com/auth/drive"]
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name("drive2.json", scope)
drive = GoogleDrive(gauth)
file1 = drive.CreateFile({"mimeType": "text/csv"})
file1.SetContentFile("test.csv")
file1.Upload()
print("finished")
The second version does not throw any error, but is not doing what I want either. What should be done here?