VBA to access and update FreeAgent data


#1

Hello,

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” & _
“&redirect_uri=http%3A%2F%2Flocalhost”

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!
Thanks,
Diane


#2

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: