Page 1 of 1

Any code snippet for getting token of OAuth2?

PostPosted: Wed Apr 03, 2024 10:03 am
by hua
Hi guys,
Any code snippet to get OAuth2.0 token without using ActiveX?


TIA

Re: Any code snipper for getting token of OAuth2?

PostPosted: Thu Apr 04, 2024 5:47 am
by Antonio Linares
Dear Hua,

Please try this code (not tested):
Code: Select all  Expand view
  // Create the XMLHTTP object
   oHTTP := CreateObject("MSXML2.XMLHTTP")

   // Specify the URL to send the request to
   cUrl := "https://example.com/oauth/token"

   // Specify the POST data
   cPostData := "grant_type=client_credentials&client_id=your_client_id&client_secret=your_client_secret"

   // Set up the request
   oHTTP:Open("POST", cUrl, .T.)
   oHTTP:SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

   // Send the request
   oHTTP:Send(cPostData)

   // Check if the request was successful
   IF oHTTP:Status = 200
      cResponse := oHTTP:ResponseText
      ? "Access Token:", cResponse
   ELSE
      ? "Failed to retrieve access token. Status code:", oHTTP:Status
   ENDIF

Re: Any code snippet for getting token of OAuth2?

PostPosted: Thu Apr 04, 2024 6:01 am
by hua
Thanks Antonio! Will give it a go