Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > API Authentication >

Problem Getting Refresh Token

Started by Martin Brawley -   in API Authentication

I am trying to recreate the Postman PKCE Flow in a vb.net forms application https://github.com/XeroAPI/Xero-Postman-Tutorial-PKCE-Edition
I have managed to do almost everything, but I have run into problems refreshing the token. My code is below ...

Private Sub btnRefreshToken_Click(sender As Object, e As EventArgs) Handles btnRefreshToken.Click

Try

Dim request As WebRequest = WebRequest.Create("https://identity.xero.com/connect/token?=")
request.Method = "POST"
request.Headers.Add("grant_type", "refresh_token")
request.ContentType = "application/json"

Dim postbody As String = ""
postbody = postbody & "grant_type=refresh_token"
postbody = postbody & "&refresh_token=" & Me.txtRefreshToken.Text
postbody = postbody & "&client_id=" & Me.txtClientID.Text

Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postbody)
request.ContentType = "application/form-data"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()

Dim response As WebResponse = request.GetResponse()
Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)
dataStream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
Console.WriteLine(responseFromServer)

jsonToken = JsonConvert.DeserializeObject(Of XeroToken)(responseFromServer)
Me.txtAccessToken.Text = jsonToken.access_token
Me.txtRefreshToken.Text = jsonToken.refresh_token

Catch ex As Exception
MsgBox("Error in btnRefreshToken_Click - " & ex.Message)
End Try

End Sub



I get a 400 - Bad Request Error at the highlighted line. My code is similar to get the initial tokens and this works fine.
Any help or pointers would be greatful

Regards
Martin
Hi Martin,

Can you please reply with the client id of your app and I'll take a look for some logs that might explain your issue?

Cheers,
Matt
 

Matthew Mortimer (Xero Staff)  

Matt, thanks for getting back to me. I actually got it sorted, but I am not sure it is the best way. My updated code is below...

Private Sub btnRefreshToken_Click(sender As Object, e As EventArgs) Handles btnRefreshToken.Click

Try

Dim request As WebRequest = WebRequest.Create("https://identity.xero.com/connect/token")
Dim dataStream As Stream

Dim strBoundary As String = "ProgressPlusTokenRefresh"

request.Method = "POST"
request.Headers.Add("grant_type", "refresh_token")
request.ContentType = "application/json"


Dim postData As String = ""
postData = "--" & strBoundary & vbCrLf
postData = postData & "Content-Disposition: form-data; name=grant_type" & vbCrLf & vbCrLf & "refresh_token" & vbCrLf
postData = postData & "--" & strBoundary & vbCrLf
postData = postData & "Content-Disposition: form-data; name=refresh_token" & vbCrLf & vbCrLf & Me.txtRefreshToken.Text & vbCrLf
postData = postData & "--" & strBoundary & vbCrLf
postData = postData & "Content-Disposition: form-data; name=client_id" & vbCrLf & vbCrLf & Me.txtClientID.Text & vbCrLf
postData = postData & "--" & strBoundary & "--" & vbCrLf

Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = "multipart/form-data;boundary=" & strBoundary
request.ContentLength = byteArray.Length
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()

Dim response As WebResponse = request.GetResponse()
Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)
dataStream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
Console.WriteLine(responseFromServer)

jsonToken = JsonConvert.DeserializeObject(Of XeroToken)(responseFromServer)
Me.txtAccessToken.Text = jsonToken.access_token
Me.txtRefreshToken.Text = jsonToken.refresh_token

Catch ex As Exception
MsgBox("Error in btnRefreshToken_Click - " & ex.Message)
End Try

End Sub
 

Martin Brawley