Search code examples
ruby-on-railsoauth-2.0http-status-code-400google-drive-api

Google Drive API watch 400 error Ruby on Rails


I'm implementing the google drive api using the OAuth2 gem (ruby on rails). I'm not using the client library because I'm also integrating other API's, so I'm trying to make all these calls as modular as possible. I'm having trouble with the this request: POST https://www.googleapis.com/drive/v2/changes/watch.

I keep getting this error:

{"errors"=>[{"domain"=>"global", "reason"=>"required", "message"=>"entity.resource"}], 
"code"=>400, "message"=>"entity.resource"}: { "error": { "errors": [ { "domain": 
"global", "reason": "required", "message": "entity.resource" } ], "code": 400, 
"message": "entity.resource" } } 

which is not very useful. It may not be Google. It could be OAuth2, but I don't think so, because the debugger gets to the response after making the connection. Well, at this point, I don't know anything, so any help is appreciated. There is THIS GUY who has exactly the same error code as me, as well as the same conclusion.

Anyway, the relevant code parts:

First, the parameters I pass to OAuth2::AccessToken's post method(I need more than 10 rep to post another link, but here is the dochttp://rdoc.info/github/intridea/oauth2/ebe4be038ec14b349682/OAuth2/AccessToken#post-instance_method)(you can click on the request method to see how the params are handled)

base_url = request.protocol + request.host_with_port
channel_id = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
body_post = {:id => channel_id,:type => 'web_hook',:address => base_url + "/googledrive/webhook"}
headers = {'Content-Type' => 'application/json'}
response = makeApiCall(token,"google_drive","/changes/watch","post",{},body_post,headers)

All this does is built my request with my the appropriate request body and headers for the call

Here is the relevant part from makeApiCall (at the line token.post is where the request is made, and where it breaks)

params = {"oauth_consumer_key" => ENV[key], "access_token" => token.token}.merge(params)
#body = Rack::Utils.build_query(body)
opts = {
    :params => params,
    :body => body,
    :headers => headers
}

if(method=="get")
    response = token.get(base + path,:opts => opts)
elsif(method=="post")
    debugger 
    response = token.post(base + path,:opts => opts)
end

This is my first or second post, so forgive me if I messed anything up.


Solution

  • Okay, after struggling for so long, I figured out why it wasn't working. I forgot to do this:

    JSON.generate(body_post)
    

    So that my body was actually in application/json form.

    Also, this line:

    response = token.post(base + path,:opts => opts)
    

    should be this:

    response = token.post(base + path,opts)
    

    simply because I'm not assigning the opts key, but the variable that I'm passing. Silly me.