Murray Brown
asked on
ASP.net Error when trying to connect to Xero accounting API using VB.net
Hi. I am using the following VB.net code in my ASP.net WebForms project to connect to the Xero accounting API.
It takes me to the Xero login and allows me to log in and then click the button to stay connected for 30 minutes
but then gives me the error in the attached image. I have a web page called AccountingXero.aspx so I am not sure what I
have done wrong
It takes me to the Xero login and allows me to log in and then click the button to stay connected for 30 minutes
but then gives me the error in the attached image. I have a web page called AccountingXero.aspx so I am not sure what I
have done wrong
Imports Xero.Api.Core
Imports Xero.Api.Infrastructure.Interfaces
Imports Xero.Api.Example.Applications.Public
Imports Xero.Api.Infrastructure.OAuth
Imports System.Collections.Concurrent
Public Class WebForm1
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
If Request.QueryString("oauth_verifier") Is Nothing Then
myxeroapi = XeroAuthenticate(Me, False, "")
Else
myxeroapi = XeroAuthenticate(Me, True, "")
If Not myxeroapi Is Nothing Then
Response.Write(myxeroapi.organisation.name)
End If
End If
End If
End Sub
Protected Function XeroAuthenticate(inpage As Page, authpagereturnedauthentication As Boolean, incode As String) As XeroCoreApi
' 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 New Consumer("xxxx", "yyyy")
Dim myxerouser = New ApiUser() With {.Name = "x@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 = "http://localhost:61795/AccountingXero.aspx"
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
End Function
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
Image1.png
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A lucky guess on my part as I've run into the same problem before.
Glad it helped. Thanks for the points!
Glad it helped. Thanks for the points!
ASKER
Yes surprisingly quick answer to something I couldn't figure out. Thanks again!
ASKER