0

I'm trying to connect to the Xero API via a Public application, and I've got a separate process that does the oAuth flow which stores off the token and token secret which I then turn around to use in this script.

My issue is when I go to use that token in a HTTParty call, I keep getting a Consumer_key_unknown error and I'm not sure why.

Script;

require 'httparty'

def nonce_generator()
  random = Random.new
  random.rand(100000000000)
end

//statics for testing
XERO_API_URL = 'https://api.xero.com/api.xro/2.0/'
TOKEN = "token received back from initial oauth process"
TOKEN_SECRET = "token secret received back from initial oauth process"
XERO_CONS_KEY = "my cons key per Xero Developer/Applications page"
XERO_CONS_SECRET = "my cons secret per Xero Developer/Applications page"
AUTH = "oauth_consumer_key=#{XERO_CONS_KEY}&
    oauth_token=#{TOKEN}&
    oauth_token_secret=#{TOKEN_SECRET}&
    oauth_signature_method=RSA-SHA1&
    oauth_timestamp=#{Time.now.to_i}&
    oauth_nonce=#{nonce_generator()}"

//test it works
xero_accounts =  HTTParty.get("#{XERO_API_URL}/accounts", :headers => { "Authorization" => AUTH }, "Accept" => "application/json")

NB: I've split AUTH across multiple lines here for ease of viewing they're all on one line in my script.

I've tried swapping AUTH out for either;

AUTH = "Token #{TOKEN}"
AUTH = "Bearer #{TOKEN}"

to no effect. I've used a similar script with the Kounta API which works via the Bearer token.

I've thought about using the Xeroizer/Xero Gateway gems but both are built with the aim of doing the oAuth for you and carrying that forward, whereas I've already done that as a separate process.

Any comments welcomed!

gorlaz
  • 468
  • 6
  • 20

1 Answers1

0

Gave up trying to sort it out that way and sifted through the gem until I found an authorise from access token method which works fine;

require 'xeroizer'

client = Xeroizer::PublicApplication.new(XERO_CONS_KEY, XERO_CONS_SECRET)
client.authorize_from_access(TOKEN, TOKEN_SECRET)

puts client.Organisation.all
gorlaz
  • 468
  • 6
  • 20