-1

I have the following command that I run in PowerShell: curl.exe -X POST https://app.inventorum.com/api/auth/token/ -u '$CLIENTID:$CLIENTSECRET' -d 'grant_type=refresh_token&refresh_token=$REFRESHTOKEN'

It returns a valid new refresh and bearer token. But I cannot for the life of me get this done in VBA in Excel:

Private Function getAccessToken() As String
Dim httpRequest As New WinHttpRequest
Dim apiUrl As String

apiUrl = "https://app.inventorum.com/api/auth/token/"

httpRequest.Open "POST", apiUrl
httpRequest.SetCredentials clientId, clientSecret, 0
httpRequest.send "grant_type=refresh_token&refresh_token=" & refreshToken

Debug.Print httpRequest.responseText
End Function

The returned response: {"error": "unsupported_grant_type"}

I don't know what I am doing wrong. I also cannot get it done in Insomnia, but this is a separate issue.

Spurious
  • 1,903
  • 5
  • 27
  • 53
  • Why is this tagged PowerShell, when your specific use case is VBA execution? Even the Command you are using is not PS, as you are using ```curl.exe``` (a cmd.exe executable, thus cmd.exe is handling this call) directly and not ```curl``` (aka Invoke-WebRequest). So, you are looking for help with VBA, not PS in any way. [What did you search for](https://duckduckgo.com/?q=%27msexcel+getaccesstoken%27&t=ffab&ia=web)? – postanote Oct 22 '22 at 20:44
  • First of all, the PowerShell implementation of `curl` is accessible under curl.exe as explained here: https://stackoverflow.com/a/30807818/1626443. Second, it's a "translation problem" because several methods produce different results and PowerShell provides me with the correct result and thus I included it. – Spurious Oct 22 '22 at 23:06
  • 1
    https://success.qualys.com/discussions/s/question/0D52L00004TnxOUSAZ/curl-api-call-in-vba – Tim Williams Oct 23 '22 at 21:37

1 Answers1

0

You could try something like this. This is a C# implementation.

using System.Net.Http.Headers;

HttpClient client = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://app.inventorum.com/api/auth/token/");

request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("$CLIENTID:$CLIENTSECRET")));

request.Content = new StringContent("grant_type=refresh_token&refresh_token=$REFRESHTOKEN");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Misunderstood
  • 5,534
  • 1
  • 18
  • 25