ASP.net Error when trying to connect to Xero accounting API using VB.net

Murray Brown
Murray Brown used Ask the Experts™
on
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

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

Open in new window

Image1.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Director, Information Systems
Commented:
Is it possible they only allow connections from your public DNS name?  Running the code in the IDE (as localhost) may break that...
Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks. You were right
Paul MacDonaldDirector, Information Systems

Commented:
A lucky guess on my part as I've run into the same problem before.  

Glad it helped.  Thanks for the points!
Murray BrownASP.net/VSTO Developer

Author

Commented:
Yes surprisingly quick answer to something I couldn't figure out. Thanks again!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial