Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ASP classic example for connecting a public app to Xero.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPNETPublicApp.Default" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
<h1>Xero Testbed</h1>
<asp:Button ID="cmdSubmit" runat="server" Text="Connect to Xero" OnClick="cmdConnect_Click" />
<asp:Button runat="server" ID="cmdCreateInvoice" Text="Create Invoice" OnClick="CreateInvoice" />
<asp:Label ID="invoiceCreatedLabel" runat="server"/>
using System;
using DevDefined.OAuth.Consumer;
using DevDefined.OAuth.Framework;
using DevDefined.OAuth.Logging;
using DevDefined.OAuth.Storage.Basic;
using XeroApi;
using XeroApi.Model;
using XeroApi.OAuth;
namespace ASPNETPublicApp
public partial class Default : System.Web.UI.Page
//After creating a public app on xero fill in these details. With this example, you must specify a callback domain when creating the app.
private const String UserAgent = "PutUserAgentHere";
private const String ConsumerKey = "PutConsumerKeyHere";
private const String ConsumerSecret = "PutConsumerSecretHere";
protected void Page_Load(object sender, EventArgs e)
//Step3. When loading the page, if the page was loaded from callback, the Request contains an oauth_verifier param.
if (Request.Params["oauth_verifier"] != null)
//We need to finish the authorisation process by exchanging our RequestToken for an AccessToken
protected void cmdConnect_Click(object sender, EventArgs e)
IOAuthSession consumerSession = new XeroApiPublicSession(UserAgent, ConsumerKey, ConsumerSecret, new InMemoryTokenRepository());
consumerSession.MessageLogger = new DebugMessageLogger();
//Step 1. Get a Request Token
var callbackUri = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port, "Default.aspx");
RequestToken requestToken = consumerSession.GetRequestToken(callbackUri.Uri);
//Step 2. Get the user to log into Xero using the request Token and redirect to the authorisation URL
var authorisationUrl = consumerSession.GetUserAuthorizationUrl();
Session["ConsumerSession"] = consumerSession;
protected void FinishAuthorisation(string verifier)
var consumerSession = (IOAuthSession)Session["ConsumerSession"];
//Exchange the RequestToken for an AccessToken using the verifier code from the callback url
AccessToken accessToken = consumerSession.ExchangeRequestTokenForAccessToken(verifier);
catch (OAuthException)
//create a repository with the authenticated session
var repository = new Repository(consumerSession);
Session["repository"] = repository;
public void CreateInvoice(object sender, EventArgs e)
//retrieve the repository from the session state
var repository = (Repository)Session["repository"];
//if the repository is null, the user must authenticate
if (repository == null)
this.invoiceCreatedLabel.Text = "There is no repository. You have not Authorized yet";
//Use the API wrapper to create an invoice
var invoice = repository.Create(
new Invoice
Type = "ACCREC",
Contact = new Contact {Name = "Bob"},
Date = DateTime.Today,
DueDate = DateTime.Today.AddDays(14),
Status = "DRAFT",
LineItems = new LineItems
new LineItem
Description = "Services Rendered",
Quantity = 1,
UnitAmount = 1,
//write the details for the invoice in the label on the home page
this.invoiceCreatedLabel.Text = String.Format("Invoice {0} was raised against {1} on {2} for {3}{4}",
invoice.Contact.Name, invoice.Date, invoice.Total,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment