Reputation: 179
I was using this website as a source to auto-generate google slides using the contents in google spreadsheet. All works fine but I cant make it save it to a folder in my google drive. Can someone help me?
I tried:
folder_id = 'xxx'
file_metadata = {'title': 'spreadsheet data DEMO','parents': {'id':folder_id}}
DATA = {'title': 'Generating slides from spreadsheet data DEMO'}
rsp = SLIDES.presentations().create(body=file_metadata).execute()
deckID = rsp['presentationId']
titleSlide = rsp['slides'][0]
titleID = titleSlide['pageElements'][0]['objectId']
subtitleID = titleSlide['pageElements'][1]['objectId']
then getting an error:
HttpError: https://slides.googleapis.com/v1/presentations?alt=json returned "Invalid JSON payload received. Unknown name "parents": Cannot find field.">
Upvotes: 2
Views: 2329
Reputation: 201398
file_metadata
, I understood like this.If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
SLIDES.presentations().create()
is used for the Slides API. In this case, file_metadata = {'title': 'spreadsheet data DEMO','parents': {'id':folder_id}}
cannot be used. The reason of your issue is this.https://www.googleapis.com/auth/drive
.Before you run the script, please update the scopes using the following flow. If your access token has this scope, it is not required to do the following flow.
https://www.googleapis.com/auth/drive
to the scopes.By this, the refresh token and access token with new scopes are retrieved and new credential file is created.
In this pattern, at first, the Google Slides is created by Slides API and the created Google Slides is moved to the specific folder.
Modified script:SLIDES = build('slides', 'v1', credentials=creds) # or SLIDES = discovery.build('slides', 'v1', http=creds.authorize(Http()))
DRIVE = build('drive', 'v2', credentials=creds) # or DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))
# Create new Google Slides using Slides API.
DATA = {'title': 'spreadsheet data DEMO'}
rsp = SLIDES.presentations().create(body=DATA).execute()
file_id = rsp['presentationId']
# Move created Google Slides to specific folder using Drive API v2.
folder_id = '###'
file_metadata = {'parents': [{'id': folder_id}]}
res = DRIVE.files().update(
fileId=file_id,
body=file_metadata,
).execute()
print(res)
If Drive API v3 is used, it becomes as follows.
DRIVE = build('drive', 'v3', credentials=creds) # or DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
folder_id = '###'
res = DRIVE.files().update(
fileId=file_id,
addParents=folder_id,
removeParents='root'
).execute()
print(res)
In this pattern, the new Google Slides is directly created to the specific folder using Drive API.
Sample script 1: Using Drive API v2DRIVE = build('drive', 'v2', credentials=creds) # or DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))
folder_id = '###'
file_metadata = {'title': 'spreadsheet data DEMO',
'parents': [{'id': folder_id}],
'mimeType': 'application/vnd.google-apps.presentation'
}
res = DRIVE.files().insert(body=file_metadata).execute()
print(res)
Sample script 2: Using Drive API v3
DRIVE = build('drive', 'v3', credentials=creds) # or DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
folder_id = '###'
file_metadata = {'name': 'spreadsheet data DEMO',
'parents': [folder_id],
'mimeType': 'application/vnd.google-apps.presentation'
}
res = DRIVE.files().create(body=file_metadata).execute()
print(res)
oauth2client
or google-auth
. So as the sample, I show DRIVE
as DRIVE = build('drive', 'v2', credentials=creds) # or DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))
. Please use DRIVE
and SLIDES
you are using by modifying the version and name.If I misunderstood your question and this was not the direction you want, I apologize.
Upvotes: 3