0

For API call I've tried..

In RestClient

Works but is wrong:

response = RestClient::Request.execute(
method: :get,
url: 'https://api.data.charitynavigator.org/v2/Organizations?app_id=2b1ffdad&app_key=XXXX',
)

Doesn't Work (403: Forbidden):

response = ::RestClient::Request.execute(method: :get, url: 'https://api.data.charitynavigator.org/v2/Organizations?app_id=2b1ffdad', 
headers: {app_key: 'XXXX'})

In HTTParty

Also doesn't work (Authentication parameters missing):

require 'rubygems'
require 'httparty'


class Charity
  include HTTParty

  base_uri 'https://api.data.charitynavigator.org/v2/'

  def posts

    headers = {
      "app_id"  => "2b1ffdad",
      "app_key"  => "XXXX"
    }

    self.class.get("/Organizations/",
    :headers => headers
    )
  end
end


charity = Charity.new
puts charity.posts

For reference: https://charity.3scale.net/docs/data-api/reference

Is it syntax? I've also looked into Faraday but ran into similar issues there. Many 3rd party API examples with rails seem to be using long outdated APIs so it's been hard piecing everything together.

Any insight would be greatly appreciated. Really want to understand this.

colemars
  • 979
  • 3
  • 12
  • 25

1 Answers1

0

I can't understand your saying Works but is wrong but Charity Navigator Data API request the sending app_id and app_key keys through the parameters, not header.

Your first code looks correct.

Second code, app_key key was sent by header, not params. So API response 403.

Third code, coded with httpart gem, dose not use parameters but headers. So Charity Navigator Data API response Authentication parameters missing error. It's normal.

require 'httparty'

class StackExchange
  include HTTParty
  base_uri 'https://api.data.charitynavigator.org/v2/'

  def posts
    options = { 
      query: {
        app_id: '2b1ffdad',
        app_key: 'XXXX'
      }
    }

    self.class.get("/Organizations/", options)
  end
end

charity = Charity.new
puts charity.posts

But you can use parameters by query option in httparty. Read the httparty docs and this SO. And you can use parameters options in rest-client gem, too. And I recommend use it strongly.

Penguin
  • 863
  • 1
  • 9
  • 22