Murray Brown
asked on
ASP.net C# web forms code to authenticate into Xero accounting
Hi
I used the following VB.net code to authenticate into Xero in ASP.net web forms. I am not sure if this is outdated but I want to either convert to C# or find the latest C# code to do the same thing
Imports Xero.Api.Core
Imports Xero.Api.Infrastructure.Interfaces
Imports Xero.Api.Example.Applications.Public
Imports Xero.Api.Infrastructure.OAuth
Imports System.Collections.Concurrent
Imports Xero.Api.Core.Model
Imports Xero.Api.Core.Model.Invoice
Imports System.Data.SqlClient
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myxeroapi As XeroCoreApi
If IsPostBack = False Then
'Response.Redirect("Invoicing")
'SO PAGE LOADS TWICE
If Request.QueryString("oauth_verifier") Is Nothing Then
'FIRST LOAD ALLOWS YOU TO AUTH IN
myxeroapi = XeroAuthenticate(Me, False, "")
Else
'SECOND LOAD LET'S YOU DO SOMETHING
myxeroapi = XeroAuthenticate(Me, True, "")
If Not myxeroapi Is Nothing Then
Dim i As Integer = myxeroapi.Contacts.Find.Count 'Quck Test
'Call oCreateInvoice(myxeroapi)
Session("myxeroapi") = myxeroapi
'Call oCreateInvoiceTest(myxeroapi)
End If
End If
End If
End Sub
Protected Function XeroAuthenticate(inpage As Page, authpagereturnedauthentication As Boolean, incode As String) As XeroCoreApi
Try
' This procedure will return w working XeroCoreApi if successful and nothing otherwise
' Set client secrets for general use and then load any override ones from the web.config file
Dim myxeroconsumer As Consumer = Nothing
If Hosting.HostingEnvironment.IsDevelopmentEnvironment Then
myxeroconsumer = New Consumer("EQRZML8NU5CHUEBTRMNNM9KA7TCISW", "G0TIIHEFVF5YKAODHIUMI8JRTQNBRV")
Else
myxeroconsumer = New Consumer("F9VC8DZYPK0DKIU0G0IIMTR9BRXPGD", "T6KA26QKZTOG3KDOKZUKK2ILBY5MDS")
End If
Dim myxerouser = New ApiUser() With {.Name = "murray@office2web.co.uk"}
' If user defined ID and Secrets exist then use those instead
If Not System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientId") Is Nothing Then
If Not System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientSecret") Is Nothing Then
If System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientId").ToString <> "" Then
myxeroconsumer = New Consumer(System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientId").ToString, System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientSecret").ToString)
End If
End If
End If
' Define Page Call Information
Dim myxerocallbackurl As String
If Hosting.HostingEnvironment.IsDevelopmentEnvironment Then
myxerocallbackurl = "http://localhost:62054/Default.aspx"
Else
myxerocallbackurl = "http://office2web.net"
End If
Dim myxeromemorystore = New MemoryAccessTokenStore()
Dim myxerorequestTokenStore = New MemoryRequestTokenStore()
Dim myxerobaseapiurl = "https://api.xero.com"
' Authenticate with Xero
Dim myxeroauthenticator As New PublicMvcAuthenticator(myxerobaseapiurl, myxerobaseapiurl, myxerocallbackurl, myxeromemorystore, myxeroconsumer, myxerorequestTokenStore)
If authpagereturnedauthentication = False Then
' Redirect to the authentication page
Dim requri As String = myxeroauthenticator.GetRequestTokenAuthorizeUrl(myxerouser.Name)
If requri <> "" Then Response.Redirect(requri, True)
Return Nothing
Else
' Validate token using querystrings that have been returned from the authentication process
Dim myxerotoken As IToken = myxeroauthenticator.RetrieveAndStoreAccessToken(myxerouser.Name, Request.QueryString("oauth_token").ToString, Request.QueryString("oauth_verifier").ToString, Request.QueryString("org").ToString)
If Not myxerotoken Is Nothing Then
Dim myxeroapi As New XeroCoreApi("https://api.xero.com", myxeroauthenticator, myxeroconsumer, myxerouser)
Return myxeroapi
Else
Return Nothing
End If
End If
Catch ex As Exception
Response.Write("Authentication Error a1: " & ex.Message)
End Try
End Function
Protected Sub btnAccountsReceivable_Click(sender As Object, e As EventArgs) Handles btnAccountsReceivable.Click
Response.Redirect("Invoicing")
End Sub
Protected Sub btnGeneral_Click(sender As Object, e As EventArgs) Handles btnGeneral.Click
Response.Redirect("General")
End Sub
Protected Sub btnPurchaseOrders_Click(sender As Object, e As EventArgs) Handles btnPurchaseOrders.Click
Response.Redirect("PurchaseOrders")
End Sub
End Class
Public Class MemoryAccessTokenStore
Implements ITokenStore
Private Shared ReadOnly _tokens As IDictionary(Of String, IToken) = New ConcurrentDictionary(Of String, IToken)()
Protected Function Find(userId As String) As IToken Implements ITokenStore.Find
Dim token As IToken = Nothing
If String.IsNullOrWhiteSpace(userId) Then
Else
_tokens.TryGetValue(userId, token)
End If
Return token
End Function
Protected Sub Add(token As IToken) Implements ITokenStore.Add
_tokens(token.UserId) = token
End Sub
Protected Sub Delete(token As IToken) Implements ITokenStore.Delete
If _tokens.ContainsKey(token.UserId) Then
_tokens.Remove(token.UserId)
End If
End Sub
End Class
Public Class MemoryRequestTokenStore
Implements ITokenStore
Private Shared ReadOnly _tokens As IDictionary(Of String, IToken) = New ConcurrentDictionary(Of String, IToken)()
Protected Function Find(userId As String) As IToken Implements ITokenStore.Find
If String.IsNullOrWhiteSpace(userId) Then
Return Nothing
End If
Dim token As IToken = Nothing
_tokens.TryGetValue(userId, token)
Return token
End Function
Protected Sub Add(token As IToken) Implements ITokenStore.Add
_tokens(token.UserId) = token
End Sub
Protected Sub Delete(token As IToken) Implements ITokenStore.Delete
If _tokens.ContainsKey(token.UserId) Then
_tokens.Remove(token.UserId)
End If
End Sub
End Class
From: henzard kruger <henzardkruger@gmail.com>
Sent: Friday, May 13, 2022 10:04 AM
To: Murray Brown <murbro9@outlook.com>
Subject: Re: Murray - ASP.net webforms C# Xero authentication needed
Hi,
Is this what you are looking for?
KWSConnector.XeroInvoices.Endpoints.zip
Regards,
Henzard Kruger Skype: henzard
Some are born great others achieve http://about.me/henzardkruger
greatness throughout their lifetime but only 0711304241
a Few have Greatness Trusted upon them. Fax : 0865011267
John 14:12-14
Timezone : GMT+2 / Harare / Pretoria / SAST
Get a suitable meeting time here
On Fri, 29 Apr 2022 at 15:42, Murray Brown <murbro9@outlook.com> wrote:
Hi Henzard.
Below is the VB.net code that I used on the Default page of my ASP.net web forms VB.net project. I will send you two files on WeTransfer. This project and also a blank C# project where I would like to authenticate into Xero on the home page. If it is easy to add a bit of code to create a simple invoice too that would be great.
Thanks
Murray
Imports Xero.Api.Core
Imports Xero.Api.Infrastructure.Interfaces
Imports Xero.Api.Example.Applications.Public
Imports Xero.Api.Infrastructure.OAuth
Imports System.Collections.Concurrent
Imports Xero.Api.Core.Model
Imports Xero.Api.Core.Model.Invoice
Imports System.Data.SqlClient
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myxeroapi As XeroCoreApi
If IsPostBack = False Then
'Response.Redirect("Invoicing")
'SO PAGE LOADS TWICE
If Request.QueryString("oauth_verifier") Is Nothing Then
'FIRST LOAD ALLOWS YOU TO AUTH IN
myxeroapi = XeroAuthenticate(Me, False, "")
Else
'SECOND LOAD LET'S YOU DO SOMETHING
myxeroapi = XeroAuthenticate(Me, True, "")
If Not myxeroapi Is Nothing Then
Dim i As Integer = myxeroapi.Contacts.Find.Count 'Quck Test
'Call oCreateInvoice(myxeroapi)
Session("myxeroapi") = myxeroapi
'Call oCreateInvoiceTest(myxeroapi)
End If
End If
End If
End Sub
Protected Function XeroAuthenticate(inpage As Page, authpagereturnedauthentication As Boolean, incode As String) As XeroCoreApi
Try
' This procedure will return w working XeroCoreApi if successful and nothing otherwise
' Set client secrets for general use and then load any override ones from the web.config file
Dim myxeroconsumer As Consumer = Nothing
If Hosting.HostingEnvironment.IsDevelopmentEnvironment Then
myxeroconsumer = New Consumer("EQRZML8NU5CHUEBTRMNNM9KA7TCISW", "G0TIIHEFVF5YKAODHIUMI8JRTQNBRV")
Else
myxeroconsumer = New Consumer("F9VC8DZYPK0DKIU0G0IIMTR9BRXPGD", "T6KA26QKZTOG3KDOKZUKK2ILBY5MDS")
End If
Dim myxerouser = New ApiUser() With {.Name = "murbro9@yahoo.com"}
' If user defined ID and Secrets exist then use those instead
If Not System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientId") Is Nothing Then
If Not System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientSecret") Is Nothing Then
If System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientId").ToString <> "" Then
myxeroconsumer = New Consumer(System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientId").ToString, System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientSecret").ToString)
End If
End If
End If
' Define Page Call Information
Dim myxerocallbackurl As String
If Hosting.HostingEnvironment.IsDevelopmentEnvironment Then
myxerocallbackurl = "http://localhost:62054/Default.aspx"
Else
myxerocallbackurl = "http://groundxero.azurewebsites.net"
End If
Dim myxeromemorystore = New MemoryAccessTokenStore()
Dim myxerorequestTokenStore = New MemoryRequestTokenStore()
Dim myxerobaseapiurl = "https://api.xero.com"
' Authenticate with Xero
Dim myxeroauthenticator As New PublicMvcAuthenticator(myxerobaseapiurl, myxerobaseapiurl, myxerocallbackurl, myxeromemorystore, myxeroconsumer, myxerorequestTokenStore)
If authpagereturnedauthentication = False Then
' Redirect to the authentication page
Dim requri As String = myxeroauthenticator.GetRequestTokenAuthorizeUrl(myxerouser.Name)
If requri <> "" Then Response.Redirect(requri, True)
Return Nothing
Else
' Validate token using querystrings that have been returned from the authentication process
Dim myxerotoken As IToken = myxeroauthenticator.RetrieveAndStoreAccessToken(myxerouser.Name, Request.QueryString("oauth_token").ToString, Request.QueryString("oauth_verifier").ToString, Request.QueryString("org").ToString)
If Not myxerotoken Is Nothing Then
Dim myxeroapi As New XeroCoreApi("https://api.xero.com", myxeroauthenticator, myxeroconsumer, myxerouser)
Return myxeroapi
Else
Return Nothing
End If
End If
Catch ex As Exception
Response.Write("Authentication Error a1: " & ex.Message)
End Try
End Function
Protected Sub btnAccountsReceivable_Click(sender As Object, e As EventArgs) Handles btnAccountsReceivable.Click
Response.Redirect("Invoicing")
End Sub
Protected Sub btnGeneral_Click(sender As Object, e As EventArgs) Handles btnGeneral.Click
Response.Redirect("General")
End Sub
Protected Sub btnPurchaseOrders_Click(sender As Object, e As EventArgs) Handles btnPurchaseOrders.Click
Response.Redirect("PurchaseOrders")
End Sub
End Class
Public Class MemoryAccessTokenStore
Implements ITokenStore
Private Shared ReadOnly _tokens As IDictionary(Of String, IToken) = New ConcurrentDictionary(Of String, IToken)()
Protected Function Find(userId As String) As IToken Implements ITokenStore.Find
Dim token As IToken = Nothing
If String.IsNullOrWhiteSpace(userId) Then
Else
_tokens.TryGetValue(userId, token)
End If
Return token
End Function
Protected Sub Add(token As IToken) Implements ITokenStore.Add
_tokens(token.UserId) = token
End Sub
Protected Sub Delete(token As IToken) Implements ITokenStore.Delete
If _tokens.ContainsKey(token.UserId) Then
_tokens.Remove(token.UserId)
End If
End Sub
End Class
Public Class MemoryRequestTokenStore
Implements ITokenStore
Private Shared ReadOnly _tokens As IDictionary(Of String, IToken) = New ConcurrentDictionary(Of String, IToken)()
Protected Function Find(userId As String) As IToken Implements ITokenStore.Find
If String.IsNullOrWhiteSpace(userId) Then
Return Nothing
End If
Dim token As IToken = Nothing
_tokens.TryGetValue(userId, token)
Return token
End Function
Protected Sub Add(token As IToken) Implements ITokenStore.Add
_tokens(token.UserId) = token
End Sub
Protected Sub Delete(token As IToken) Implements ITokenStore.Delete
If _tokens.ContainsKey(token.UserId) Then
_tokens.Remove(token.UserId)
End If
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.