Reputation: 453
I am working on a Ruby program to access my google drive in order to get my files. I want to use the server-to-server connection to avoid people to accept permissions, so I use the p12 key generated for the Service Account Client ID. The connection is ok but and I inserted a file successfully.
My problem is when I try to list all my files: The call works well but I only get 2 files: the previously inserted document, and a pdf called "How to get started with Drive" (I have a lot of other file on mine, like docs, spreadsheet, presentations, pictures, etc...).
I created my key with my google account, but I can't access my files. Also, the document I inserted with my program doesn't appear in my google drive among my files.
Is there something I missed about the API? What file I am getting instead of mine? It's really like I am not on the same Google Drive account. Here is the code I use to list my files:
# Prepare the OAuth client / Service Account
client = Google::APIClient.new(application_name: 'Google Drive Ruby test', application_version: '0.0.1')
key = Google::APIClient::KeyUtils.load_from_pkcs12('key.p12', 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/drive',
:issuer => '***@developer.gserviceaccount.com',
:signing_key => key)
client.authorization.fetch_access_token!
# Get the Drive API
drive = client.discovered_api('drive', 'v2')
# Upload a file
api_result = client.execute(
:api_method => drive.files.list,
:parameters => [])
Thank you very much!
Julian
Upvotes: 3
Views: 1501
Reputation: 22306
Ahh, that's the problem. A Service Account is not your Drive account that you would update via your browser. See Google Drive help required access to own Drive account and How do I authorise an app (web or installed) without user intervention? (canonical ?). btw, in your code the comment above files.list says "Upload a file" which is why I missed it first time :-)
Upvotes: 2