0

I have a script running on my computer that should be able to manage my Google Drive content without asking me every time to connect.

From what I read on the internet, I should use service account credentials what I did.

The problem is I can't find any documentation / code example of how I should emplement my code to manage the content of the Drive ( create, delete, edit files etc...).

I have the following code:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

key_file_location = 'creds.json'
scopes = [
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/drive.file'
]

credentials = ServiceAccountCredentials.from_json_keyfile_name(
            key_file_location, scopes=scopes)
# https://developers.google.com/drive/api/v3/quickstart/python
service = build('drive', 'v3', credentials=credentials)

# Call the Drive v3 API
results = service.files().list(
    pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])

if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print(u'{0} ({1})'.format(item['name'], item['id']))

This gives me the list of all the folder and files but I can't find a way to manage the files and folders. I was looking for something like service.files().create() but can't find anything...

Would love to get some help !

1 Answers1

0

How to Access Certain Google sheets:

def sheet_access(worksheet_name, sheet_name):
    scope = ['https://www.googleapis.com/auth/spreadsheets', "https://www.googleapis.com/auth/drive.file",
             "https://www.googleapis.com/auth/drive"]
    creds = ServiceAccountCredentials.from_json_keyfile_name('your json file path', scope)
    client = gspread.authorize(creds)
    sheet = client.open(worksheet_name).worksheet(sheet_name)
    return sheet
Div_OR
  • 33
  • 9
  • Thank a lot but that's not what I am looking for... I am trying to manage files and folders through pydrive and without having to give permission every time my code asks for it... This is just for managing a google sheet with the google sheet api, not the drive one.... @Div_OR – Lorraine Cousteau Aug 01 '21 at 11:57
  • can [this](https://stackoverflow.com/questions/30524700/how-to-upload-files-to-another-users-google-drive-without-asking-permission-eve) be what you are looking for – Div_OR Aug 01 '21 at 12:02
  • somehow i manage to get to a post that i already saw, but I can't figure out how to generate the "mycreds.txt" file... I can generate the json credentials file from the Oauth Client but with both the json and the txt version of it I get an error: KeyError: '_module the link to the article I found: https://stackoverflow.com/questions/24419188/automating-pydrive-verification-process – Lorraine Cousteau Aug 02 '21 at 07:20