Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > API Authentication >

create XeroCoreApi with saved access or request tokens asp.net

Started by Judy Walsh -   in API Authentication

Hi,

I'm using the code below (from "Sample code to authenticate with the Xero-Net api on webforms (asp.net) - VB.NET Sample Code WORKING") (using a public app)
It works for me and creates a XeroCoreApi which I can use if I do it all within one website.

However in my system, authentication is completed in a web site and I store the Request tokens (or AccessTokens if that is a better idea) encrypted in a local database.

The Interaction with XERO is subsequestly done in a separate dll and at another time. I would like to use the Request/Access token I have stored to create a new XeroCoreApi, this requires a parameter "auth As IAuthenticator", how do I create/set the IAuthenticator with existing request or access tokens rather than redirecting to XERO?

Is there a different way to create a XeroCoreApi with existing tokens without using the Authenticator or by some other method?

Should I be doing something like refreshing the access token?


My asp.NET code is as follows

Dim myxeroconsumer As New Consumer(consumerKey value , consumerSecret value)
Dim myxerouser = New ApiUser() With {.Name = "MYUSER"}

' Define Page Call Information
Dim myxerocallbackurl = "http://localhost:49956/Xeroreturn.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


Any help/advice would be appreciated.

Thanks,

Judy