public
Last active

ASP classic example for connecting a public app to Xero.

  • Download Gist
Default.aspx
ASP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPNETPublicApp.Default" %>
 
 
 
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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" />
 
</div>
<p>
 
<asp:Label ID="invoiceCreatedLabel" runat="server"/>
 
</p>
</form>
</body>
</html>
Default.aspx.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
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
FinishAuthorisation(Request.Params["oauth_verifier"]);
}
 
 
}
 
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;
 
Response.Redirect(authorisationUrl);
 
 
}
 
protected void FinishAuthorisation(string verifier)
{
var consumerSession = (IOAuthSession)Session["ConsumerSession"];
try
{
//Exchange the RequestToken for an AccessToken using the verifier code from the callback url
AccessToken accessToken = consumerSession.ExchangeRequestTokenForAccessToken(verifier);
}
catch (OAuthException)
{
throw;
}
//create a repository with the authenticated session
var repository = new Repository(consumerSession);
 
Session["repository"] = repository;
 
Response.Redirect("~/");
}
 
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";
return;
}
 
//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.InvoiceNumber,
invoice.Contact.Name, invoice.Date, invoice.Total,
invoice.CurrencyCode);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.