3

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

Julian Le Calvez
  • 453
  • 7
  • 27
  • paste the code doing the files.list and ideally the http trace. Are you correctly following nextPageToken? When you say "my files" you *do* realise that a service account is NOT your files, but a separate account? – pinoyyid Mar 13 '15 at 18:18
  • This is the full code I am testing. I just print the api_result.data object. When I say my files, I want to access the spreadsheets or docs that I created via my browser. – Julian Le Calvez Mar 13 '15 at 19:01
  • 1
    Ahh, that's the problem. A Service Account is *not* your Drive account that you would update via your browser. See http://stackoverflow.com/questions/28784575/google-drive-help-required-access-to-own-drive-account/28789719#28789719 and http://stackoverflow.com/questions/19766912/how-do-i-authorise-a-background-web-app-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. – pinoyyid Mar 14 '15 at 03:02
  • Oh sorry for the comment. It's just because I tried an upload first, and then the list but I didn't change the comment :) Thanks for the links, I will try the OAuth Playground! – Julian Le Calvez Mar 14 '15 at 10:07
  • @pinoyyid thank you for the links. It works well like that!! If you want to put it as an answer, i will accept it ;) – Julian Le Calvez Mar 14 '15 at 10:38

1 Answers1

2

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 :-)

Community
  • 1
  • 1
pinoyyid
  • 21,499
  • 14
  • 64
  • 115