2013-02-27 71 views
1

我在Rails中使用twitter API的twitter gem。所有工作正常,但我无法发送图像的URL和我的帖子链接到Twitter网站.. 这是我的代码:Twitter的API,发送图像的URL,红宝石

在型号: -

def twitter 
    @twitter ||= Twitter::Client.new(oauth_token: token, oauth_token_secret: secret) 
end 

def post_to_twitter(user) 
    begin 
    @twitter = user.authentications.find_by_provider('twitter') 
    if @twitter.present? 
     twitter = @twitter.twitter 
     twitter.update(self.title) 
    end 
    rescue Exception => e 
    Rails.logger.debug e.message 
    end 
end 

在控制器: -

def create  
    @post = Post.new(params[:post]) 
    @post.user_id = current_user.id 
    respond_to do |format| 
    if @post.save   
     @post.post_to_twitter current_user   
     format.js { render :layout => false } 
    else   
    @message = "Please enter content" 
    format.js { render :layout => false } 
    end 
end 

通过使用:twitter.update(self.title)...我的应用程序的标题去到twitter站点,但我怎么能发送任何链接和图像的URL相同的标题?

回答

0

定期发送POST请求使用AccessToken发布链接和url-oauth rdoc

通过Using the oauth ruby gem获取访问令牌。

全部和全部process of Getting to grips with the Ruby OAuth gem and the Twitter API

+0

感谢您的回答Jain..one更多的问题,我怎么能发送网址到Twitter(如:本地主机url),所以,如果我点击它,它会重定向到我的应用 – 2013-02-27 13:19:02

+0

你不必明确发送此重定向网址。相反,您需要在路线上定义返回网址,并且Twitter API会识别您的应用程序的哪个网址需要重定向到您的应用程序。 – 2013-02-27 13:24:11

+0

快速了解本地测试 - 阅读本文 - http://www.tonyamoyal.com/2009/08/17/how-to-quickly-set-up-a-test-for-twitter-oauth-authentication - 从 - 您-LOCAL-MACHINE / – 2013-02-27 13:31:45