Home > Enterprise >  Method not found: 'System.Threading.Tasks.Task`1<IdentityModel.Client.TokenResponse>
Method not found: 'System.Threading.Tasks.Task`1<IdentityModel.Client.TokenResponse>

Time:06-29

I have a problem where the below code at

XeroToken = Await c.RefreshAccessTokenAsync(XeroToken).ConfigureAwait(False)

throws the

Method not found: 'System.Threading.Tasks.Task`1<IdentityModel.Client.TokenResponse> IdentityModel.Client.HttpClientTokenRequestExtensions.RequestRefreshTokenAsync(System.Net.Http.HttpMessageInvoker, IdentityModel.Client.RefreshTokenRequest, System.Threading.CancellationToken)'

Dim c = New XeroClient(XeroConfig)
...
Dim utcTimeNow = DateTime.UtcNow
If utcTimeNow > Me.XeroToken.ExpiresAtUtc Then
   XeroToken = Await c.RefreshAccessTokenAsync(XeroToken).ConfigureAwait(False)
   Dim json = New JavaScriptSerializer().Serialize(XeroToken)
   Dim token As String = Me.SaveToken(json)
End If

I've read all the posts I could find here related to the subject, made sure there were no reference do different version of the same package. I cleaned the whole solution, deleted bin and obj folders, build again (it builds and deploys just fine) but to no avail.

Our version of Visual Studio was updated recently and this caused problems with packages and references (most of the projects were full of errors) but those were resolved and everything seems to work on the surface.

Any ideas how to stop the above from throwing that error?

CodePudding user response:

You are much better off ditching the IdentityModel stuff. It's dead simple to write your own lightweight replacement:

In my code below, YourSettingsStorage is your own class for storing and getting settings within your app. I strongly recommend implementing this in such a way that Xero tokens are stored encrypted.

I am only including code here for handling the refresh. But it is not difficult to write similar code for getting the initial Xero token and refresh token.

You will need to install NewtonSoft.JSON from NUGET, or use a similar JSON serializer / deserializer.

Public Class XAT
    Public Property XERO_access_token As String = ""
    Public Property XERO_tenant_id As String = ""
End Class

Public Class XeroToken
    Public Property id_token As String
    Public Property access_token As String
    Public Property expires_in As Integer
    Public Property token_type As String
    Public Property refresh_token As String
    Public Property scope As String
End Class

Public Shared Function GetXeroAccessToken() As XAT
    Dim Result As New XAT
    Dim XERO_expires As Date = YourSettingsStorage.GetSetting("XERO_expires", Date.MinValue)
    Dim XERO_access_token As String = YourSettingsStorage.GetSetting("XERO_access_token", "")
    Dim XERO_tenant_id As String = YourSettingsStorage.GetSetting("XERO_tenant_id", Guid.Empty).ToString
    Dim XERO_id_token As String = YourSettingsStorage.GetSetting("XERO_id_token", "")
    Dim XERO_refresh_token As String = YourSettingsStorage.GetSetting("XERO_refresh_token", "")
    If XERO_expires > Date.MinValue And XERO_access_token <> "" And XERO_id_token <> "" Then
        ' Expired token?
        If XERO_expires <= Now Then
            ' Refresh the token
            Dim Headers As New Net.WebHeaderCollection
            Headers.Add("authorization", "Basic " & Convert.ToBase64String(Text.Encoding.UTF8.GetBytes(YourSettingsStorage.GetSetting("XeroClientId") & ":" & YourSettingsStorage.GetSetting("XeroClientSecret"))))
            Headers.Add("Content-Type", "application/x-www-form-urlencoded")
            Dim Err As Net.WebException = Nothing
            Dim IDToken As String = HTTPPostAndResponse("https://identity.xero.com/connect/token", "grant_type=refresh_token&refresh_token=" & XERO_refresh_token, Err, Headers)
            Dim Token As XeroToken = Newtonsoft.Json.JsonConvert.DeserializeObject(Of XeroToken)(IDToken)
            YourSettingsStorage.SetSetting("XERO_id_token", Token.id_token)
            YourSettingsStorage.SetSetting("XERO_access_token", Token.access_token)
            YourSettingsStorage.SetSetting("XERO_expires_in", Token.expires_in)
            YourSettingsStorage.SetSetting("XERO_token_type", Token.token_type)
            YourSettingsStorage.SetSetting("XERO_refresh_token", Token.refresh_token)
            YourSettingsStorage.SetSetting("XERO_scope", Token.scope)
            YourSettingsStorage.SetSetting("XERO_expires", Date.Now.AddSeconds(Token.expires_in - 60))
            XERO_access_token = YourSettingsStorage.GetSetting("XERO_access_token", "")
        End If
    End If
    Result.XERO_access_token = XERO_access_token
    Result.XERO_tenant_id = XERO_tenant_id
    Return Result
End Function

CodePudding user response:

The problem was finally solved by removing all the Xero NuGet packages and installing them again. I hope this may be helpful to someone else as well.

  • Related