Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > Getting Started >

Oaut2.0 RequestAccessTokenAsync not working

Started by Margie Caramat -   in Getting Started


Hoping anyone can assist me with the issue I am having.

I am creating a sample app to test the new Oauth2.0 authentication before applying it to our product.

But I can't get past this line in my code. If I put a debugger on this line, it just swallowed it and do nothing.

Dim xeroToken As XeroOAuth2Token = Await xeroClient.RequestAccessTokenAsync(code)

If I run my app, I can see that it is redirecting the user to the Xero login and authentication page, then it goes back to the redirect URI (httphandler) I setup and then just stops on the line of code I mentioned above.

Thanks and looking forward to any assistance.

Please note my redirect URI is an HttpHandler, Below is the content of my HttpHandler file.

Imports System.Web
Imports Newtonsoft.Json
Imports System.Configuration
Imports System.Net.Http
Imports Xero.NetStandard.OAuth2.Client
Imports Xero.NetStandard.OAuth2.Config
Imports Xero.NetStandard.OAuth2.Token
Imports Xero.NetStandard.OAuth2.Models
Imports System.Collections.Generic
Imports System.Threading.Tasks

Public Class XeroCallbackHandler
Implements IHttpHandler, System.Web.SessionState.IRequiresSessionState

Public ReadOnly Property IsReusable As Boolean Implements IHttpHandler.IsReusable
Return False
End Get
End Property

Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest

Dim strCode As String = HttpContext.Current.Request.Item("code")
If Not String.IsNullOrEmpty(strCode) Then

Dim objTask As Task(Of String) = Nothing
objTask = ProcessCallBack(strCode)

End If

End Sub

Protected Async Function ProcessCallBack(ByVal code As String) As Task(Of String)

Dim xeroClient = GetXeroClient()

Dim xeroToken As XeroOAuth2Token = Await xeroClient.RequestAccessTokenAsync(code) 'THIS IS WHERE THE EXECUTION JUST STOPS

'These are the other lines of code I used but still the same outcome.
'Dim xeroToken As XeroOAuth2Token = Await xeroClient.RequestAccessTokenAsync(code).ConfigureAwait(False)
'Dim xeroToken As IXeroToken = Await xeroClient.RequestAccessTokenAsync(code)

Dim tenants As List(Of Tenant) = Await xeroClient.GetConnectionsAsync(xeroToken)
Dim firstTenant As Tenant = tenants(0)


Return "Success"

End Function

Private Function GetXeroClient() As XeroClient

Dim objClient As New HttpClient

Dim xeroConfig As New XeroConfiguration() With {
.ClientId = "MY CLIENT ID",
.ClientSecret = "MY CLIENT SECRET",
.CallbackUri = New Uri("https://localhost:44312/XeroCallbackHandler"),
.Scope = "openid profile email offline_access files accounting.transactions accounting.contacts",
.State = "myState"

Return New XeroClient(xeroConfig)

End Function
End Class

Hi Margie - sorry it is hard to know what is going on without a stack trace. I think your best bet is to follow the tutorial for that repo (https://devblog.xero.com/getting-started-with-xero-net-sdk-6357b510d5e5) - at first glance the line of code in question `Dim xeroToken As XeroOAuth2Token = Await xeroClient.RequestAccessTokenAsync(code)` does not look correct.

From the readme there is some alternate punctuation which might be causing your problem:

await client.RequestAccessTokenAsync(code);

Christopher Knight (Xero Staff)  

Hi Christopher,

Thank you for your reply. There was no syntax error or anything that I can see from my end. Also I am using VB.net that is why the syntax is different.

Without the error or anything, can you please advise how can I get a stacktrace exposed.

Thank you.

Margie Caramat  

Hi Margie - a stack trace will be dependent on your development environment and how you are running your local webserver. I recommend starting a case by emailing api@xero.com, with your client id, time of error, and error message.

They will be able to find and report back what is the issue in the end API log.

Christopher Knight (Xero Staff)  

Thank you Christopher. I will do that.

Margie Caramat