Google drive API – ClientError – insufficientFilePermissions

I want to have access to a user’s google drive, create a folder and save files in the user’s drive. I have a client request where I get a token and save it on my db, then use it to create a Google drive service and then hopefully create a folder and a file.
I created a project on google cloud, created and enabled API key, created Auth2.0 credentials and set authorized js origins and Authorized redirect URIs
to http://localhost:3000, and http://localhost for developing, got Client Id and Client secret.

I got a pop up window that asks me to sign in for google, and another pop up that asks confirm you want to sign to the app as the user. (I never got a pop up that asks for google drive permissions). after allowing, I get the token, creating the server, I do have the @service object which seems ok, but when I try to create a folder I have insufficient permission error.
I tried to change the scope but I can see that /drive is the least restricting.

Didn’t find any other solution for my problem, would be happy for help for what is missing. Thanks!

This is my client request, first I’m getting the user’s token –

  google.accounts.id.initialize({
    client_id: '<%= ENV["GOOGLE_CLIENT_ID"] %>',
    scope: 'https://www.googleapis.com/auth/drive',
    callback: handleCredentialResponse,
    error_callback: myErrorCallback
  });
  google.accounts.id.prompt();

Then in handleCredentialResponse I save the token in my DB successfully.

This is my Google drive service –

require 'googleauth'
require 'google/apis/drive_v3'
class GoogleDriveService  
 def initialize
  @drive = Google::Apis::DriveV3 # Alias the module
  @service = @drive::DriveService.new
  @client_id = ENV['GOOGLE_CLIENT_ID']
  @client_secret = ENV['GOOGLE_CLIENT_SECRET']
  @service.key = ENV['GOOGLE_API_KEY']
  @user_token = User.current.company.properties.dig(:content_definitions, :google_token)
  @service.authorization = get_credentials(@client_id, @client_secret, @user_token)
 end
...
  def get_credentials(client_id, client_secret, user_token)
    client_secrets = Google::Auth::ClientId.new(client_id, client_secret)
    
    # Create an access token from the user token
    token = Google::Auth::UserRefreshCredentials.new(
      client_id: client_secrets.id,
      client_secret: client_secrets.secret,
      scope: 'https://www.googleapis.com/auth/drive',
      access_token: user_token
    )
    
    # Authorize the Drive service using the access token
    @service.authorization = token
    
    # Return the authorized Drive service object
@service

end

Then here, when I try @service.list_files I get an insufficient permissions error –

  def get_or_create_folder(folder_name)
    # Check if the folder already exists
    query = "mimeType='application/vnd.google-apps.folder' and trashed=false and name='#{folder_name}'"
    debugger
    results = @service.list_files(q: query, fields: 'nextPageToken, files(id, name)')
    files_list = results.files
    
    if results.any?
      # Return the existing folder
      return results.first.id
    else
      # Create a new folder
      folder_metadata = Google::Apis::DriveV3::File.new(name: folder_name, mime_type: 'application/vnd.google-apps.folder')
      folder = service.create_file(folder_metadata, fields: 'id')
      return folder.id
    end

end