Hi Pepe,
Here is an example using HttpClient to retrieve the json. You still need to parse the json to get the token, I've just shown the request to get the actual json from the token server. I'll also add an example using the OAuthClient from your original example. Both had the same issue, the arguments needed to be url encoded. I thought the OAuthClient may have done that automatically but it doesn't seem to. That may be a bug, or an enhancement request that could be made. In your example only client_secret needed to be url encoded because it contained a + character, but if the client_id had a special character it would also need url encoding.
HttpClient lhc_Client
String ls_ClientID, ls_Secret, ls_Url, ls_PostData
String ls_ResponseStatusText, ls_ResponseBody
Long ll_ResponseStatusCode, ll_return
string ls_headers
lhc_Client = Create HttpClient
ls_Url = "https://amtrip.amfresh.com/i/connect/token";
ls_ClientID = "agrox"
ls_Secret = "*agrox+"
// Need to urlencode the arguments to the form post to handle any special characters (e.g. + converts to %2B)
Blob lblb_ClientID, lblb_Secret
CoderObject lnv_CoderObject
lnv_CoderObject = Create CoderObject
lblb_ClientID = Blob(ls_ClientID, EncodingANSI!)
ls_ClientID = lnv_CoderObject.UrlEncode(lblb_ClientID)
lblb_Secret = Blob(ls_Secret, EncodingANSI!)
ls_Secret = lnv_CoderObject.UrlEncode(lblb_Secret)
Destroy lnv_CoderObject
// Set the request headers
lhc_Client.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
// Create the body (the arguments have been url encoded above)
ls_PostData = 'grant_type=client_credentials'
ls_PostData += '&client_id=' + ls_ClientID
ls_PostData += '&client_secret=' + ls_Secret
ll_return = lhc_Client.SendRequest("POST", ls_Url, ls_PostData)
If ll_return = 1 Then
ll_ResponseStatusCode = lhc_Client.GetResponsestatusCode()
ls_ResponseStatusText = lhc_Client.GetResponsestatusText()
lhc_Client.GetResponseBody(ls_ResponseBody, EncodingUTF8!)
ls_headers = lhc_Client.GetResponseHeaders()
MessageBox('Token Response', string(ll_ResponseStatusCode) + ' ' + ls_ResponseStatusText + '~r~n' + ls_ResponseBody)
else
MessageBox("SendRequest Failed", "Return :" + String ( ll_return ))
end if
destroy lhc_Client
This is a modified example of your original OAuthClient code. You'll just need to tidy it up a bit. Have a look at how the returns are handled. I usually check the return from a http call (e.g. AccessToken() or SendRequest()) separately to the check for ResponseStatusCode. If the first one fails, it means that it didn't get any return from the server (e.g. timeout, invalid url etc.) which is different from getting a return from the server that is an error (e.g. a 400 Bad Request)
OAuthClient loac_Client
TokenRequest ltr_Request
TokenResponse ltr_Response
OAuthRequest loar_Request
ResourceResponse lrr_Response
String ls_AccessToken
String ls_Body, ls_type, ls_description, ls_state, ls_uri
Long ll_return
String ls_ClientID, ls_Secret, ls_Url
// Setup the arguments for your token request
ls_Url = "https://amtrip.amfresh.com/i/connect/token";
ls_ClientID = "agrox"
ls_Secret = "*agrox+"
// Need to urlencode the arguments to the form post to handle any special characters (e.g. + converts to %2B)
// For this example you really only need it for ls_Secret but I've shown it for both arguments
Blob lblb_ClientID, lblb_Secret
CoderObject lnv_CoderObject
lnv_CoderObject = Create CoderObject
lblb_ClientID = Blob(ls_ClientID, EncodingANSI!)
ls_ClientID = lnv_CoderObject.UrlEncode(lblb_ClientID)
lblb_Secret = Blob(ls_Secret, EncodingANSI!)
ls_Secret = lnv_CoderObject.UrlEncode(lblb_Secret)
Destroy lnv_CoderObject
// Now do your token request
loac_Client = Create OAuthClient
//Step 1: Get the RESTful server access token.
ltr_Request.ClearParams()
ltr_Request.tokenlocation = ls_Url
ltr_Request.granttype = "client_credentials"
ltr_Request.Method = "POST"
ltr_Request.clientid = ls_ClientID
ltr_Request.clientsecret = ls_Secret
ll_Return = loac_Client.AccessToken( ltr_Request, ltr_Response )
If ll_Return = 1 then
// The Actual Http Call was successful
long ll_ResponseStatusCode
string ls_ResponseStatusText
ll_ResponseStatusCode = ltr_Response.GetStatusCode()
ls_ResponseStatusText = ltr_Response.GetStatusText()
ltr_Response.GetBody(ls_Body)
if ll_ResponseStatusCode = 200 Then
// The Token Request was successful
ls_AccessToken = ltr_Response.GetAccessToken()
MessageBox('Token', ls_AccessToken)
else
// The Token Request returned an error. This displays the actual html returned
MessageBox('Token Error', string(ll_ResponseStatusCode) + ' ' + ls_ResponseStatusText + '~r~n' + ls_Body)
// You can have a look at this function. When the secret is wrong ls_type returns "invalid_client"
ll_Return = ltr_Response.GetTokenError(ls_type, ls_description, ls_uri, ls_state)
MessageBox( "AccessToken Failed", "Return :" + String ( ll_return ) + "~r~n" + ls_type )
end if
Else
// The Actual Http Call was unsuccessful
MessageBox( "AccessToken Call Failed", "Return :" + String ( ll_return ))
// From help file the error will be one of the following:
// -1 -- A general error occurred
// -2 -- Invalid URL
// -3 -- Cannot connect to the Internet
// -4 -- Timeout
End If
If IsValid ( loac_Client ) Then DesTroy ( loac_Client )
Here is my approach using httpclient,also from Appeon Documentation:
HttpClient lhc_Client
CoderObject lco_Code
Jsonpackage ljpg_json
String ls_ClientID, ls_Sercet, ls_Auth, ls_Url, ls_PostData, ls_UserName, ls_Password, ls_scope, ls_Body, ls_Error
String ls_Token, ls_TokenType, ls_AccessToken
Blob lblb_data
Long ll_return
lhc_Client = Create HttpClient
lco_Code = Create CoderObject
ljpg_json = Create Jsonpackage
//Step 1: Get the RESTful server access token.
//Url
ls_Url = "https://amtrip.amfresh.com/i/connect/token"
//Authorization
ls_ClientID = "agrox"
ls_Sercet = "*agrox+"
lblb_data = Blob ( ls_ClientID + ":" + ls_Sercet, EncodingUTF8! )
ls_Auth = lco_Code.Base64Encode( lblb_data )
lhc_Client.SetRequestHeader( "Authorization", "Basic " + ls_Auth )
lhc_Client.SetRequestHeader( "Content-Type", "application/x-www-form-urlencoded" )
ls_PostData = "grant_type=client_credentials" + ls_auth
ll_return = lhc_Client.SendRequest( "POST", ls_Url, ls_PostData )
If ll_return = 1 And lhc_Client.GetResponsestatusCode() = 200 Then
lhc_Client.GetResponseBody ( ls_body )
ls_Error = ljpg_json.loadString ( ls_body )
If ls_Error = "" then
ls_TokenType = ljpg_json.GetValue("token_type")
ls_Token = ljpg_json.GetValue("access_token")
ls_AccessToken = ls_TokenType + " " + ls_Token
Else
MessageBox( "Error", ls_Error )
End If
Else
MessageBox( "AccessToken Falied", "Return :" + String ( ll_return ) + "~r~n" + lhc_Client.GetResponsestatusText() )
End If
If IsValid ( lco_Code ) Then DesTroy ( lco_Code )
If IsValid ( ljpg_json ) Then DesTroy ( ljpg_json )
If IsValid ( lhc_Client ) Then DesTroy ( lhc_Client )
return ls_AccessToken