1

I'm using the ruby curb gem and trying to call this api in the code. But, I'm not sure how to pass the -u parameter.

curl -X GET -H 'Accept: application/json' -u 111122223333: https://api.somesite.com/v1/apps/e876c39/users/activities?updated_since=20130925T120000Z

Here's the code I have so far, but it doesn't work without the -u option. I tried adding it as a parameter, but doesn't work.

url = "https://api.somesite.com/v1/apps/e876c39/users/activities?updated_since=20130925T120000Z"

curl = Curl::Easy.new(url)
curl.headers['Accept'] = 'application/json'
curl.headers['Content-Type'] = 'application/json'
curl.username = ENV['APP_ID'].to_s + ':'
result = curl.perform
user2974739
  • 619
  • 1
  • 7
  • 20

2 Answers2

0

It looks like you're trying to pass both username and password (with the : delimiter) to the username= method, but glancing at the docs it looks like that method is for setting the username only (not a big surprise). If you want to set both the username and password with the colon syntax, it looks like userpwd= is the method you want:

easy.userpwd = string => string

Set the username/password string to use for subsequent calls to perform. The supplied string should have the form "username:password"

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • I tried that, but still get the message curl.body_str {"message":"Not authorized"} – user2974739 Oct 21 '15 at 22:18
  • What output do you get in stderr if you set `curl.verbose = true`? – Jordan Running Oct 21 '15 at 22:20
  • * Connected to api.humanapi.co (54.69.45.169) port 443 (#0) * TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 * Server certificate: *.humanapi.co * Server certificate: DigiCert SHA2 Secure Server CA * Server certificate: DigiCert Global Root CA > GET /v1/apps/11111/users/activities?updated_since=20130925T120000Z HTTP/1.1 Host: api.humanapi.co Accept: application/json Content-Type: application/json – user2974739 Oct 21 '15 at 22:23
  • < HTTP/1.1 401 Unauthorized < Content-Type: application/json; charset=utf-8 < Date: Wed, 21 Oct 2015 22:26:17 GMT < Server: nginx < X-Powered-By: Express < Content-Length: 28 < Connection: keep-alive – user2974739 Oct 21 '15 at 22:23
  • I'm not sure where the issue is, sorry. Given that it's SSL, it could be a lot of things. – Jordan Running Oct 21 '15 at 22:24
  • The curl command works fine on the command line. How do you call the curl command in ruby without using curl::easy? – user2974739 Oct 21 '15 at 22:25
  • You can use `system` or `exec`: http://stackoverflow.com/questions/6338908/ruby-difference-between-exec-system-and-x-or-backticks – Jordan Running Oct 21 '15 at 22:34
  • How do you store the results from the system command to a variable? I tried result = Kernel.system “curl ...") but the value in result is just true whereas the curl command returned json data. – user2974739 Oct 21 '15 at 22:52
0

I was able to do this using RestClient

require 'rest_client'    

response = RestClient::Resource.new "https://api.humanapi.co/v1/apps/#{ENV['CLIENT_ID']}/users/activities?updated_since=20130925T120000Z", "#{ENV['APP_ID']}:", ""

results = JSON.parse response.get
user2974739
  • 619
  • 1
  • 7
  • 20