-1

I run the following command 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.

How can this be 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"}

3
  • 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? Commented Oct 22, 2022 at 20:44
  • First of all, the PowerShell implementation of curl is accessible under curl.exe as explained here: 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. Commented Oct 22, 2022 at 23:06
  • 1
    success.qualys.com/discussions/s/question/0D52L00004TnxOUSAZ/… Commented Oct 23, 2022 at 21:37

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.