Giter Club home page Giter Club logo

Comments (2)

itsaphel avatar itsaphel commented on August 16, 2024 1

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.

matthewbloch avatar matthewbloch commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.