google-api-ruby-client icon indicating copy to clipboard operation
google-api-ruby-client copied to clipboard

Unable to create folder

Open maclarensg opened this issue 8 years ago • 5 comments

I tried to create a folder in my drive

file_metadata = {
    name: 'gBackup',
    mime_type: 'application/vnd.google-apps.folder'
  }
  file = service.create_file(file_metadata, fields: 'id')
  puts "Folder Id: #{file.id}"

However, i have errors about permission. I am able to list files

Traceback (most recent call last):
	15: from ./googledrive.rb:61:in `<main>'
	14: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.24.2/generated/google/apis/drive_v3/service.rb:601:in `create_file'
	13: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.24.2/lib/google/apis/core/base_service.rb:360:in `execute_or_queue_command'
	12: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.24.2/lib/google/apis/core/http_command.rb:93:in `execute'
	11: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
	10: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
	 9: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable'
	 8: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.24.2/lib/google/apis/core/http_command.rb:101:in `block in execute'
	 7: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
	 6: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
	 5: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable'
	 4: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.24.2/lib/google/apis/core/http_command.rb:104:in `block (2 levels) in execute'
	 3: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.24.2/lib/google/apis/core/http_command.rb:299:in `execute_once'
	 2: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.24.2/lib/google/apis/core/http_command.rb:183:in `process_response'
	 1: from /home/gavinyap/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.24.2/lib/google/apis/core/api_command.rb:116:in `check_status'
/home/gavinyap/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.24.2/lib/google/apis/core/http_command.rb:218:in `check_status': insufficientPermissions: Insufficient Permission (Google::Apis::ClientError)

maclarensg avatar Oct 06 '18 14:10 maclarensg

Sorry my bad. My scope was define incorrectly

maclarensg avatar Oct 06 '18 15:10 maclarensg

How did you define the correct scope?

seanriordan08 avatar Dec 15 '19 20:12 seanriordan08

@maclarensg what was the correct format that worked for you? Thanks!

npendery avatar Apr 27 '21 18:04 npendery

The scope is define at authorization stage. See. https://github.com/googleworkspace/ruby-samples/blob/master/drive/quickstart/quickstart.rb which had a Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY scope, which should be oogle::Apis::DriveV3::AUTH_DRIVE_METADATA instead

Do note I was using Google Api Drive V2 back then in 2018. but is about the same.

maclarensg avatar Apr 27 '21 19:04 maclarensg

Hi @maclarensg,

I've got the same error even though I use the Google::Apis::DriveV3::AUTH_DRIVE_METADATA

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
    json_key_io: File.open(SERVICE_ACCOUNT_PKCS12_FILE_PATH),
    scope: Google::Apis::DriveV3::AUTH_DRIVE_METADATA)

drive_service = Google::Apis::DriveV3::DriveService.new
drive_service.client_options.application_name = APPLICATION_NAME
drive_service.authorization = authorizer

folder_metadata = {
    name: folder_name,
    mime_type: 'application/vnd.google-apps.folder'
}
folder = drive_service.create_file(folder_metadata, fields: 'id')

I created a GCP project, then created a service account, enabled Google Drive API for the project. I was able to upload a file with that service account. But when I tried to create a folder, I faced permission error.

Do you have any idea how to grant the permission of creating folder to the service account. I looked around but couldn't file.

Thanks,

ngvdong avatar Aug 27 '22 10:08 ngvdong