0

I'm have a python script that gets data from a csv myFile.csv file and pushes it into a google drive folder.

When i run my code, I get an error

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=multipart returned "Insufficient Permission: Request had insufficient authentication scopes.". Details: "[{'domain': 'global', 'reason': 'insufficientPermissions', 'message': 'Insufficient Permission: Request had insufficient authentication scopes.'}]">

What I'm I missing?

Below is my code

from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
import google.auth

scope = [
  'https://www.googleapis.com/auth/drive.file',
  'https://www.googleapis.com/auth/drive.resource',
  'https://spreadsheets.google.com/feeds',
  'https://www.googleapis.com/auth/drive',
  'https://www.googleapis.com/auth/drive.readonly']

creds, _ = google.auth.default(scopes=scope)

def push_csv_to_google_drive(creds):

  service = build('drive', 'v3', credentials=creds)
  file_metadata = {"name": 'myFile.csv', "parents": [gdrive_destination_folder_id]}
  media = MediaFileUpload(
      source_csv_file_path, 
      mimetype="file/csv")
        
  file = service.files().create(
          body=file_metadata, 
          media_body=media, 
          fields="id").execute()

if __name__ == '__main__':
    push_csv_to_google_drive(creds=creds)
3
  • is this all your code? your not storing the token anywhere? Commented Dec 5, 2022 at 18:58
  • This is all the code, no use of service account. @DaImTo Commented Dec 5, 2022 at 19:06
  • Yeah but where is your authorization? Commented Dec 5, 2022 at 20:10

1 Answer 1

1

As far as i can see you are not authorizing a user at all.

This is my drive upload sample.

#   To install the Google client library for Python, run the following command:
#   pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib


from __future__ import print_function

import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/drive']


def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('tokenDriveUpload.json'):
        creds = Credentials.from_authorized_user_file('tokenDriveUpload.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'C:\YouTube\dev\credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('tokenDriveUpload.json', 'w') as token:
            token.write(creds.to_json())

    try:
        # create drive api client
        service = build('drive', 'v3', credentials=creds)

        file_metadata = {'name': 'Upload.csv'}
        media = MediaFileUpload('Upload.csv',
                                mimetype='text/plain')
        # pylint: disable=maybe-no-member
        file = service.files().create(body=file_metadata, media_body=media,
                                      fields='id').execute()
        print(F'File ID: {file.get("id")}')

    except HttpError as error:
        # TODO(developer) - Handle errors from drive API.
        print(F'An error occurred: {error}')



if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.