Based on SO here, we are creating a pdf from a Google Document. The pdf is uploaded to Google Drive.
self.GOOGLEDOCUMENTS_SCOPES = ['https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata']
# Set the permission for the google sheet
self.documents_permission = {
'role' : 'reader',
'type' : 'anyone',
'value' : 'anyone'
}
document_credentials, document_project_id =
auth.default(scopes=self.GOOGLEDOCUMENTS_SCOPES) # Returns a tuple.
self.document_credentials = document_credentials
self.document_project_id = document_project_id
We create the google document using the Google Doc API
title = "file name for doc" body = { 'title': title, } document_service = build('docs', 'v1', credentials=self.document_credentials) create_document_response = document_service.documents().create(body=body).execute() document_Id = create_document_response.get("documentId")We then use export_media to create a pdf of the google doc
media_pdf_request = drive_service.files().export_media(fileId=document_Id, mimeType="application/pdf") file = io.BytesIO() downloader = MediaIoBaseDownload(file, media_pdf_request) done = False while done is False: status, done = downloader.next_chunk() file_content = file.getvalue() file_metadata = {'name' : filename_statement,'mimeType': 'application/pdf'} media = MediaIoBaseUpload(io.BytesIO(file_content),mimetype='application/pdf', resumable=True) pdf_file = ( drive_service.files() .create(body=file_metadata, media_body=media, fields="id") .execute() ) pdf_file_name = pdf_file.get("id") doc_res = drive_service.permissions().create(fileId=pdf_file_name, body=self.documents_permission, fields='id').execute() pdf_file_id = doc_res.get('id') statement_link = "https://docs.google.com/document/d/" + document_Id drive_link = "https://drive.google.com/drive/" + pdf_file_name
We expect the file to be accessible at
drive_link = "https://drive.google.com/drive/" + pdf_file_name
Unfortunately we get a 404 error. Please help.
UPDATE -
We changed the drive link to the following -
drive_link = "https://www.googleapis.com/drive/v3/files/" + pdf_file_name
But we get at 403 error
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"errors": [
{
"message": "The request is missing a valid API key.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}