VBA to access and update FreeAgent data


I am new to all this, so be gentle :blush: I have been trying use Excel to manage my data. I want to be able to import data from FreeAgent, change it and load it back in. I also want to be able to load new data. However, I have been banging my head against the wall and not even managed the first hurdle.

I have successfully obtained an authorization code which I now want to swop for tokens (access and refresh) using VBA. I have tried several different iterations of the code and I have tried to start from refreshing my authorization code too (no luck :frowning:) and keep getting the same error message returned,:

“HTTP Basic: Access denied”

Please see my (one of many attempted and failed) code below (I’m hoping it is something obvious - I have xxx for sensitive values, all these have been triple checked):

Sub CommandButton1_Click()

Dim TargetURL As String
TargetURL = “https://api.freeagent.com/v2/token_endpoint

Dim getTokenBody As String
getTokenBody = “grant_type=authorization_code” & _
“&code=xxx” & _

Dim authUser As String
authUser = “xxx”

Dim authPass As String
authPass = “xxx”

Set HTTPReq = CreateObject(“WinHttp.WinHttpRequest.5.1”)
HTTPReq.Option(4) = 13056 ’
HTTPReq.Open “POST”, TargetURL, False
HTTPReq.SetCredentials authUser, authPass, 0
HTTPReq.setRequestHeader “Content-Type”, “application/x-www-form-urlencoded”
HTTPReq.send (getTokenBody)
MsgBox (HTTPReq.responseText)

End Sub

Any help is welcome!

Ok, think I figured this one - the redirect uri was not https - now seems to work! :smile:

Onto trying to code the refresh :cold_sweat:

Hi @dfielding did you ever complete your script? Would you mind sharing?

Hi all. Think you might be interested in my post - managed to get the refresh token to work: