Unable to create folder
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)
Sorry my bad. My scope was define incorrectly
How did you define the correct scope?
@maclarensg what was the correct format that worked for you? Thanks!
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.
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,