如何在 Rails 应用程序中使用 httparty 的基本身份验证? [英] How to use basic authentication with httparty in a Rails app?

查看:45
本文介绍了如何在 Rails 应用程序中使用 httparty 的基本身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带有基本身份验证的httparty"命令行版本工作起来简单而出色:

The command line version of 'httparty' with basic authentication works simple and great:

httparty -u username:password http://example.com/api/url

但现在我正在寻找可以从 Rails 应用程序中将基本身份验证添加到 HTTParty.get 调用的方法.首先,出于测试目的,我想对控制器中的登录凭据进行硬编码.只是为了确保它有效.但是我找不到任何文档或示例如何传递这些内容.

But now I'm looking for the way I can add the basic auth to a HTTParty.get call from within a Rails app. First of all, for testing purposes, I want to hard code the login credentials in the Controller. Just to make sure it works. But I can't find any documentation or examples how you can pass these along.

没有凭据的 HTTParty.get 工作正常:

A HTTParty.get without credentials works fine:

@blah = HTTParty.get("http://twitter.com/statuses/public_timeline.json")

但我不知道如何对此进行更改以接受 -u username:password 部分.

But I don't see how I can make a variation on this that accepts the -u username:password part.

对我来说下一个挑战(我对 Ruby/Rails 非常陌生)是从用户表单中获取用户凭据并动态传递它,但现在对我来说最重要的是让硬编码版本工作.

The next challenge for me (am very new to Ruby/Rails) is to get the user credentials from a user form and pass it along dynamically, but most important for me now it to get the hard coded version to work.

推荐答案

auth = {:username => "test", :password => "test"}
@blah = HTTParty.get("http://twitter.com/statuses/public_timeline.json", 
                     :basic_auth => auth)

这篇关于如何在 Rails 应用程序中使用 httparty 的基本身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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