Question

In my Rails 3 app, I am able to successfully authenticate using Oauth2 and able to get the metadata for a file. The downloadUrl is

https://doc-10-3o-docs.googleusercontent.com/docs/securesc/tj647mo7q16s2rquitcrcv800pkn7gcf/ap67p147th03cn8rjpu68i8qva3p7i8j/1345240800000/02289284805103305740/02289284805103305740/0BwsQ03A3DXbCTVBjUDlNNzNJNDQ?h=16653014193614665626&e=download&gd=true

The documentation states that I must do the following:

Gets a file's metadata by ID. To download a file's content, send an authorized HTTP GET request to the file's downloadUrl

I do not wish to use Google APIs Client Library for Ruby, but simply formulate a HTTP Request using HTTParty

Here is a snippet of the code I've been trying to get to work

response = HTTParty.get(https://doc-10-3o-docs.googleusercontent.com/docs/securesc/tj647mo7q16s2rquitcrcv800pkn7gcf/ap67p147th03cn8rjpu68i8qva3p7i8j/1345240800000/02289284805103305740/0?access_token={token})
open("/User/mymachine/test.pdf", 'wb'){|pdf| pdf << response.body}

I'm pretty sure I'm formulating the request wrong. Any help would be greatly appreciated. Thanks in advance.

Was it helpful?

Solution

You have to add the Authorization: Bearer header to your request, together with the access token you retrieved during the OAuth 2.0 flow.

Basically, your HTTP request must look like the one in the OAuth 2.0 documentation:

https://developers.google.com/accounts/docs/OAuth2WebServer#callinganapi

I'm not a Ruby expert, but the request using HTTParty should be:

response = HTTParty.get(downloadUrl, :headers => {"Authorization" => "OAuth {token}"})

Where downloadUrl is the one you got from the file's metadata and token is the access token you retrieved when performing authorization.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top