Ruby/HTTParty:使用其 API 将提要添加到 Google Reader 时超时
我尝试使用 Google Reader 的 API 添加订阅,但收到以下错误:
执行已过期
我在读取(使用“获取”)订阅或标签列表时没有遇到任何问题。但当我尝试添加子项(使用“post”)时,它会超时。
代码是用 Ruby on Rails 编写的,并且我使用 HTTParty 来处理与 Web 服务的通信。
我的代码如下(我对 Ruby/Rails 还很陌生,所以对下面包含的任何不好的做法感到抱歉。我很高兴他们向我指出):
class ReaderUser
# Include HTTParty - this handles all the GET and POST requests.
include HTTParty
...
def add_feed(feed_url)
# Prepare the query
url = "http://www.google.com/reader/api/0/subscription/quickadd?client=scroll"
query = { :quickadd => feed_url, :ac => 'subscribe', :T => @token }
query_as_string = "quickadd=#{CGI::escape(feed_url)}&ac=subscribe&T=#{CGI::escape(@token.to_s)}"
headers = { "Content-type" => "application/x-www-form-urlencoded; charset=UTF-8", "Content-Length" => query_as_string.length.to_s, "Authorization" => "GoogleLogin auth=#{@auth}" }
# Execute the query
self.class.post(url, :query => query, :headers => headers)
end
...
end
作为参考,这就是我获取令牌:
# Obtains a token from reader
# This is required to 'post' items
def get_token
# Populate @auth
get_auth
# Prepare the query
url = 'http://www.google.com/reader/api/0/token'
headers = {"Content-type" => "application/x-www-form-urlencoded", "Authorization" => "GoogleLogin auth=#{@auth}" }
# Execute the query
@token = self.class.get(url, :headers => headers)
end
# Obtains the auth value.
# This is required to obtain the token and for other queries.
def get_auth
# Prepare the query
url = 'https://www.google.com/accounts/ClientLogin'
query = { :service => 'reader', :Email => @username, :Passwd => @password }
# Execute the query
data = self.class.get(url, :query => query)
# Find the string positions of AUTH
auth_index = data.index("Auth=") + 5
# Now extract the values of the auth
@auth = data[auth_index,data.length]
end
我很乐意提供所需的任何其他信息。
提前致谢!
I'm attempting to add a subscription to Google Reader, using it's API, however I'm getting the following error:
execution expired
I've had no problems reading (using 'get') a list of subscriptions or tags. But it times out when I attempt to add a sub (using 'post')
The code is written in Ruby on Rails and I'm using HTTParty to handle the communication with the web service.
My code is as follows (I'm still new to Ruby/Rails so sorry for any bad practices included below. I'm more than happy to have them pointed out to me):
class ReaderUser
# Include HTTParty - this handles all the GET and POST requests.
include HTTParty
...
def add_feed(feed_url)
# Prepare the query
url = "http://www.google.com/reader/api/0/subscription/quickadd?client=scroll"
query = { :quickadd => feed_url, :ac => 'subscribe', :T => @token }
query_as_string = "quickadd=#{CGI::escape(feed_url)}&ac=subscribe&T=#{CGI::escape(@token.to_s)}"
headers = { "Content-type" => "application/x-www-form-urlencoded; charset=UTF-8", "Content-Length" => query_as_string.length.to_s, "Authorization" => "GoogleLogin auth=#{@auth}" }
# Execute the query
self.class.post(url, :query => query, :headers => headers)
end
...
end
For reference, this is how I'm obtaining the token:
# Obtains a token from reader
# This is required to 'post' items
def get_token
# Populate @auth
get_auth
# Prepare the query
url = 'http://www.google.com/reader/api/0/token'
headers = {"Content-type" => "application/x-www-form-urlencoded", "Authorization" => "GoogleLogin auth=#{@auth}" }
# Execute the query
@token = self.class.get(url, :headers => headers)
end
# Obtains the auth value.
# This is required to obtain the token and for other queries.
def get_auth
# Prepare the query
url = 'https://www.google.com/accounts/ClientLogin'
query = { :service => 'reader', :Email => @username, :Passwd => @password }
# Execute the query
data = self.class.get(url, :query => query)
# Find the string positions of AUTH
auth_index = data.index("Auth=") + 5
# Now extract the values of the auth
@auth = data[auth_index,data.length]
end
I'd be happy to provide any additional information required.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番折腾,终于找到了解决办法!
我只需将内容长度设置为“0”。以前,我根据我所基于的 PHP 类将其设置为“查询”的长度(greader.class.php)。我提到这一点只是为了防止其他人遇到同样的问题。
After a great deal of messing around, I've found the solution!
I simply had to set the Content-Length to "0". Previously I was setting it to the length of the 'query' as per the PHP class I was basing it on (greader.class.php). I mention this just in case someone else has the same problem.