Giter VIP home page Giter VIP logo

xero-netstandard's Introduction

Xero OAuth2 .Net SDK

This code is generated from the openapi-generator based on Xero OpenAPI 3.0 Specification

NuGet.org

Current release of SDK with OAuth2 support

Version Xero-NetStandard SDK only supports OAuth2 authentication and the following API sets.

  • accounting
  • identity

Coming soon

  • bank feeds
  • projects
  • payroll
  • files
  • fixed asset
  • xero hq

How to handle OAuth 2.0 authentication & authorization?

We have built Xero OAuth 2.0 Client. Check out Xero.NetStandard.OAuth2Client

NuGet.org

To learn more about how our OAuth 2.0 flow works and how to use the OAuth 2.0 client, check out our Xero developer blog post: Up and running with .NET and Xero OAuth 2.0

Looking for previous SDK with oAuth 1.0a support?

Codebase, samples and setup instructions located in oauth1 branch.

Getting Started

Create a Xero App

Follow these steps to create your Xero app

  • Create a free Xero user account (if you don't have one)
  • Use this URL for beta access to oAuth2 https://developer.xero.com/myapps
  • Click "New app" link
  • Enter your App name, company url, privacy policy url, and redirect URI (this is your callback url - localhost, etc)
  • Agree to terms and condition and click "Create App".
  • Click "Generate a secret" button.
  • Copy your client id and client secret and save for use later.
  • Click the "Save" button. You secret is now hidden.

Installation

Use Nuget to download the package

	dotnet add package Xero.NetStandard.OAuth2
	dotnet add package Xero.NetStandard.OAuth2Client

or using the Package Manager Console inside Visual Studio

	Install-Package Xero.NetStandard.OAuth2
	Install-Package XXero.NetStandard.OAuth2Client

or you can download the source code from https://github.com/XeroAPI/Xero-NetStandard and compile it by yourself.

To get started there are a few main classes.

  • The AccountingApi class
  • XeroOAuth2

The AccountingApi class is not coupled to the OAuth2 class, so feel free to use your own.

To get started you will just need two things to make calls to the Accounting Api.

  • xero-tenant-id
  • accessToken

Build the login link

	XeroConfiguration xconfig = new XeroConfiguration();
    
	xconfig.ClientId = "yourClientId";
	xconfig.ClientSecret = "yourClientSecret";
	xconfig.CallbackUri = new Uri("https://localhost:5001") //default for standard webapi template
	xconfig.Scope = "openid profile email files accounting.transactions accounting.contacts offline_access";
    
	var client = new XeroClient(xconfig);
	
	//build login link
	Console.WriteLine(client.BuildLoginUri());
	
	//In the example app, I've used the config via appsettings so it becomes:
	var client = new Xero.NetStandard.OAuth2.Client.XeroClient(config.Value, httpClientFactory);

From here the user will be redirected to login, authorise access and get redirected back

On the way back you will get a parameter with code and state

  • code
  • state
	XeroConfiguration xconfig = new XeroConfiguration(); 
    
	xconfig.ClientId = "yourClientId";
	xconfig.ClientSecret = "yourClientSecret";
	xconfig.CallbackUri = new Uri("https://localhost:5001") //default for standard webapi template
	xconfig.Scope = "openid profile email files accounting.transactions accounting.contacts offline_access";
	
	var client = new XeroClient(xconfig);
	
	//before getting the access token please check that the state matches
	await client.RequestAccessTokenAsync(code, "yourState");
    
	//from here you will need to access your Xero Tenants
	List<Tenant> tenants = await client.GetConnections();
    
	// you will now have the tenant id and access token
	foreach (Tenant tenant in tenants)
	{
		// do something with your tenant and access token
		//client.AccessToken;
		//tenant.TenantId;
	}

To refresh your token. Just call the refresh

	xeroClient.RefreshTokenAsync(xeroToken); //use the latest token you have
	//Here is a full example using a webapi
	[HttpGet]
	public async Task<ActionResult> Index(string code, string state)
	{
		var client = new XeroClient(config.Value, httpClientFactory);
		var xeroToken = (XeroOAuth2Token)await client.RequestXeroTokenAsync(code);
		TokenUtilities.StoreToken(Environment.MachineName, xeroToken);
		return RedirectToAction("Invoices", "Accounting");
	}

To setup the main API object see the snippet below

	var AccountingApi = new AccountingApi();
	var response = await AccountingApi.GetInvoicesAsync(accessToken, xeroTenantId);
	
	Console.WriteLine(AccountingApi.GetInvoices().ToJson();)

TLS 1.0 deprecation

As of June 30, 2018, Xero's API will remove support for TLS 1.0.

The easiest way to force TLS 1.2 is to set the Runtime Environment for your server (Tomcat, etc) to Java 1.8 which defaults to TLS 1.2.

License

This software is published under the MIT License.

Copyright (c) 2019 Xero Limited

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

xero-netstandard's People

Contributors

antonberezan avatar cbs-greg avatar chappoo avatar ezescan-rhys avatar fnakashima avatar gerald-abacus avatar ian-hook avatar jamescolemanxero avatar jenksguo avatar jordwest avatar justabearoz avatar ken-santoso avatar kensantoso avatar kiwidave72 avatar matthewsteeples avatar mjava1 avatar mjmortimer avatar mpbelov avatar mrrrk avatar nickcoad avatar orthros avatar paulblamire avatar philals avatar richard-rowley avatar rileyjamesxero avatar rohitsies avatar ronanq avatar sidneyallen avatar tamaw avatar theregan avatar

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.