Comments (2)
Thanks @matthewbloch. It's a huge 😳 moment but, reading your example above and testing in curl (which worked, but I was trying a different endpoint), I realised my issue was the OAuth scopes I was requesting. I think my authorisation URL code originally looked something like:
def get_xero_auth_url
get_xero_client.authorize_url(
redirect_uri: admin_root_url + "/xero_callback",
scope: "accounting.settings.read offline_access"
)
end
which of course isn't the most useful of scopes, and so the requests for any meaningful data were being rejected. Changed that and all seems good now :D
from xeroizer.
I know the example in the README.md has a typo and isn't complete, so it's not surprising that you're having a problem with making a minimal example.
However your code is also not complete :) If you're still stuck can you maybe paste an entire controller here that doesn't work, as the devil will be in the detail e.g. you might have a bug in how you're reloading the session between requests.
Here's the complete, minimal "works for me" example with Sinatra which I've not yet had a chance to contribute to the repo:
require 'active_support/isolated_execution_state' # https://github.com/waynerobinson/xeroizer/issues/555
require 'sinatra'
require 'xeroizer'
# Get these parameters at https://developer.xero.com/app/manage/
#
XERO_CLIENT_ID = "xxx"
XERO_SECRET = "xxx"
# https://developer.xero.com/documentation/oauth2/scopes
# N.B. must have offline_access for token refresh to work
XERO_SCOPE = %w(
accounting.settings.read
accounting.transactions
accounting.contacts.read
offline_access
).
join(" ")
# Make sure this matches the "Redirect URI" - can be localhost for testing!
#
XERO_REDIRECT="http://localhost:3000/redirect"
# Generate this with e.g. openssl rand -base64 30
SESSION_SECRET="xxx"
configure do
enable :sessions
set :session_secret, SESSION_SECRET
end
helpers do
def save_xero_to_session
session[:xero][:access_token] = @xero.access_token.token
session[:xero][:refresh_token] = @xero.access_token.refresh_token
session[:xero][:expires_at] = @xero.access_token.expires_at
session[:xero][:tenant_id] = @xero.current_connections.first.tenant_id
end
def xero
@xero = Xeroizer::OAuth2Application.new(
XERO_CLIENT_ID,
XERO_SECRET,
session[:xero] ? {
tenant_id: session[:xero][:tenant_id],
access_token: session[:xero][:access_token],
refresh_token: session[:xero][:refresh_token]
} : {})
if session[:xero] && Time.now+60 > Time.at(session[:xero][:expires_at])
@xero.renew_access_token
save_xero_to_session
end
@xero
end
end
get '/login' do
# Redirect user to Xero login page
redirect xero.authorize_url(redirect_uri: XERO_REDIRECT, scope: XERO_SCOPE)
end
get '/redirect' do
# Xero sends user back here once authenticated
xero.authorize_from_code(params[:code], redirect_uri: XERO_REDIRECT)
save_xero_to_session
redirect '/invoices'
end
get '/invoices' do
[200, {"content-type" => "text/plain"}, xero.Invoice.all(order: 'Date').inspect]
end
from xeroizer.
Related Issues (20)
- Question: Does Xeroizer support OAuth 2? HOT 2
- `.sent_to_contact` on Invoices is undefined HOT 3
- Update the readme/documentation to include Oauth2Application HOT 2
- Xero Deprecation Notice - OAuth 1.0a HOT 1
- Timeout option not set correctly on OAuth 2 client HOT 3
- invoice.save returns false but invoice.errors is empty HOT 15
- New gem release? HOT 6
- How to get all the Items from Xero account HOT 1
- NameError: uninitialized constant Xeroizer::OAuth2Application HOT 4
- Double raise for expired tokens? HOT 2
- GET ERROR WHE do any object.save HOT 2
- trying to run tests as a developer... HOT 5
- syntax error, unexpected ',' in branding_theme.rb HOT 1
- Line Amount can not explicity be set to 0.00 if there is a discount
- Missing require somewhere: "uninitialized constant ActiveSupport::XmlMini::IsolatedExecutionState" when used outside of Rails HOT 3
- ApiExceptions result in parse error
- Saving Quotes
- BankAccount Model Not Available?
- How to revoke token / delete current_connection HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xeroizer.