2011-05-09 45 views
1

我试图订阅添加到谷歌阅读器,使用它的API添加饲料时,谷歌阅读器,但我发现了以下错误:红宝石/ HTTParty:定时出使用其API

execution expired

我在阅读(使用'get')订阅或标签列表时没有任何问题。但是,当我试图添加一个子(使用'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 

我很乐意提供所需的任何其他信息。

在此先感谢!

回答

0

经过大量的搞乱之后,我找到了解决方案!

我只需将Content-Length设置为“0”即可。之前,我将它设置为'查询'的长度,根据我基于它的PHP类(greader.class.php)。我提到这一点,以防别人遇到同样的问题。