Google Drive API观看400错误Ruby on Rails [英] Google Drive API watch 400 error Ruby on Rails

查看:103
本文介绍了Google Drive API观看400错误Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OAuth2 gem(ruby on rails)实现了谷歌驱动器api。我没有使用客户端库,因为我还集成了其他API,所以我试图尽可能模块化所有这些调用。我无法处理此请求: POST https://www.googleapis。 com / drive / v2 / changes / watch



我一直收到这个错误:

  {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}}

这不是很有用。它可能不是Google。它可能是OAuth2,但我不这么认为,因为调试器在连接后获得响应。那么,在这一点上,我什么都不知道,所以任何帮助表示赞赏。 这个GUY 的人完全一样错误代码和我一样,以及相同的结论。

无论如何,相关代码部分:

首先,我传递给OAuth2 :: AccessToken的post方法的参数(我需要超过10个rep来发布另一个链接,但这里是dochttp://rdoc.info/github/intridea/oauth2/ebe4be038ec14b349682/OAuth2/AccessToken#post- instance_method)(你可以点击request方法来查看params是如何处理的)。

$ p $ 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)

所有这些都是通过我的合适的请求主体和标头来构建我的请求。

这里是来自makeApiCall的相关部分(在线token.post是请求发生的地方,以及它发生什么地方)

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

if(method ==get)
response = token.get(base + path,:opts => opts)
elsif方法==post)
调试器
响应= token.post(base + path,:opts => opts)
结束

这是我的第一篇或第二篇文章,所以如果我把任何东西搞砸了,请原谅我。 解决方案

好吧,经过这么长时间的努力,我找出了为什么它不起作用。我忘了这么做:

  JSON.generate(body_post)

因此,我的身体实际上是以application / json形式。



另外,这一行:

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

应该是这样的:

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

只是因为我没有分配opts键,而是变量我正在通过。傻我。


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.

解决方案

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.

这篇关于Google Drive API观看400错误Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆