B4J Question How to use httpJob to get rest Api Token

imbault

Well-Known Member
Licensed User
Longtime User
I'm wondering how to set b4j code in order to get Api Token
Here is the Postman Get access Token:

1667822184315.png


Thanks a lot

Patrick
 

Attachments

  • 1667648532830.png
    1667648532830.png
    24.5 KB · Views: 172
Last edited:

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Hi imbault is a bit difficult to understand what is postman sending. You may be able to access the raw request. That would be easier to help you with.

Look for the console
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Here is the Postman raw request and Response when your press
1667711491661.png



B4X:
POST https://sso-dev.art-informatique.com/auth/realms/recette/protocol/openid-connect/token200
149 ms
▶Network

▶Request Headers
Content-Type: application/x-www-form-urlencoded
Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
User-Agent: PostmanRuntime/7.29.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 00000000000000000000000000
Host: sso-dev.art-informatique.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 29

▶Request Body
grant_type: "client_credentials"

▶Response Headers
Server: nginx
Date: Sun, 06 Nov 2022 05:09:09 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-store
Set-Cookie: KC_RESTART=; Version=1; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Max-Age=0; Path=/auth/realms/recette/; Secure; HttpOnly
X-XSS-Protection: 1; mode=block
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
Referrer-Policy: no-referrer
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Content-Encoding: gzip

▶Response Body
{"access_token":"eyJhbGciOiJSUzUxMiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ1MzdVek9la2lhWVFZSDZhdlU0TFI3cHpMX0pJbXRWZ05IMnhYaDRGeUY4In0.eyJleHAiOjE2Njc3MTE2NDksImlhdCI6MTY2NzcxMT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YuwWu6mu-Fyh0LV84qOpQDEbzGdk8LOBjV0jIJRLjKT30QjPd0uKQK6HCWemkQ24DePp7BK0bwKNQrFQozJZXpy_PSIK2sy0KcmFAplJIFVbe2c551FYD2P8ZJMGUgTNSHFTM1m23ew","expires_in":300,"refresh_expires_in":0,"token_type":"Bearer","not-before-policy":0,"scope":"email profile"}
 
Last edited:
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
should be like this:
B4X:
    Dim j As HttpJob
    j.Initialize("",Me)
    j.PostString("https://sso-dev.art-informatique.com/auth/realms/recette/protocol/openid-connect/token200",$"grant_type: "client_credentials""$)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Authorization","Basic aaaaaa")
   
    wait for (j) done (j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
a few tiny errors:
1) event is "jobdone", not "done"
2) remove "200" from end of url
3) request body is "grant_type=client_credentials", not "grant_type:client_credentials". "=", not ":"

i got: {"access_token":"eyJhbGciOiJSUzUxMiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ1MzdVek9la2lhWVFZSDZhdlU0TFI3cHpMX0pJbXRWZ05IMnhYaDRGeUY4In0.eyJleHAiOjE2Njc3NDk3ODgsImlhdCI6MTY2Nzc0OTQ4OCwianRpIjoiNzcxZWVhNGMtMTFiNy00ODM0LTkzYWMtMGE2NWYxOGFmOTYzIiwiaXNzIjoiaHR0cHM6Ly9zc28tZGV2LmFydC1pbmZvcm1hdGlxdWUuY29tL2F1dGgvcmVhbG1zL3JlY2V0dGUiLCJhdWQiOiJhY2NvdW50Iiwic3ViIjoiNzBmOWEwZjMtYTBmOS00OTc2LWE1N2YtYWM3ODgwMzY2MWQwIiwidHlwIjoiQmVhcmVyIiwiYXpwIjoic2dzT2lnbmllcyIsImFjciI6IjEiLCJhbGxvd2VkLW9yaWdpbnMiOlsiaHR0cHM6Ly9haXguYXJ0LWluZm9ybWF0aXF1ZS5jb20iXSwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbIm9mZmxpbmVfYWNjZXNzIiwidW1hX2F1dGhvcml6YXRpb24iXX0sInJlc291cmNlX2FjY2VzcyI6eyJzZ3NPaWduaWVzIjp7InJvbGVzIjpbIkFQSV9BTkFMWVNFIl19LCJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50IiwibWFuYWdlLWFjY291bnQtbGlua3MiLCJ2aWV3LXByb2ZpbGUiXX19LCJzY29wZSI6ImVtYWlsIHByb2ZpbGUiLCJjbGllbnRIb3N0IjoiMjQuMjUwLjMzLjI1MSIsImNsaWVudElkIjoic2dzT2lnbmllcyIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwicHJlZmVycmVkX3VzZXJuYW1lIjoic2VydmljZS1hY2NvdW50LXNnc29pZ25pZXMiLCJjbGllbnRBZGRyZXNzIjoiMjQuMjUwLjMzLjI1MSJ9.VKnHM4FAftG8aj8pv6YwFhZmIrr5PE9uzyHDM0HidneLQxT_syIWBFO64eOEmhLcWNsMk1BP_L8c5is1EFIaqnJs5g7q9QybS9jhn8ZLBpLsuRU9Hfyn7RlAxb011wxQI7mY8t1rEMNOURrPJOGPxKkIpaOECpula_uePww4IB5_c6Pz3XojTXdPYxIWYTEHOUfbwKSQvI-ipd6iQQUerhCu2CfJki_dn_3En3FZ-x1zmuJQdFkyHjffdqlEe5nGfgg90Mr9Excy-2-UUifb8cK40UeYZgEbxEtgIGA4vK3YpLjH-V2SYkg1oxJ6dpC6oyUckyAR1Chz0RfaYQ0Z6w","expires_in":300,"refresh_expires_in":0,"token_type":"Bearer","not-before-policy":0,"scope":"email profile"}

B4X:
    Dim granttype As String = "grant_type=client_credentials"
    Dim j As HttpJob
    j.Initialize("",Me)
    j.PostString("https://sso-dev.art-informatique.com/auth/realms/recette/protocol/openid-connect/token",granttype)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Authorization","Basic c2dzT2lnbmllczpiNjIwMzI3Yy0wNTY5LTRhMDAtOTA1Yy1iNGM3MmM5NmU0MmM=")
    wait for (j) jobdone (j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
so this is a real api key?
uh @imbault , very bad idea to do this, you should ask for deletion of the post
Dear @EnriqueGonzalez , I sent you a "coffee"

can you please help me with this one :

B4X:
POST https://aix.art-informatique.com/qualibase/rest/v1/analyse/integration
404
19 ms
Network
Request Headers
Authorization: Bearer eyJhbGciOiJSUzUxMiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ1MzdVek9la2lhWVFZSDZhdlU0TFI3cHpMX0pJbXRWZ05IMnhYaDRGeUY4In0.eyJleHAiOjE2Njc4NDEzNjYsImlhdCI6MTY2Nzg0MTA2NiwianRpIjoiYjU4Y2UxNzEtNjcxZC00MDI3LTk0OTktMjkxOThlMDZiMTczIiwiaXNzIjoiaHR0cHM6Ly9zc28tZGV2LmFydC1pbmZvcm1hdGlxdWUuY29tL2F1dGgvcmVhbG1zL3JlY2V0dGUiLCJhdWQiOiJhY2NvdW50Iiwic3ViIjoiNzBmOWEwZjMtYTBmOS00OTc2LWE1N2YtYWM3ODgwMzY2MWQwIiwidHlwIjoiQmVhcmVyIiwiYXpwIjoic2dzT2lnbmllcyIsImFjciI6IjEiLCJhbGxvd2VkLW9yaWdpbnMiOlsiaHR0cHM6Ly9haXguYXJ0LWluZm9ybWF0aXF1ZS5jb20iXSwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbIm9mZmxpbmVfYWNjZXNzIiwidW1hX2F1dGhvcml6YXRpb24iXX0sInJlc291cmNlX2FjY2VzcyI6eyJzZ3NPaWduaWVzIjp7InJvbGVzIjpbIkFQSV9BTkFMWVNFIl19LCJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50IiwibWFuYWdlLWFjY291bnQtbGlua3MiLCJ2aWV3LXByb2ZpbGUiXX19LCJzY29wZSI6ImVtYWlsIHByb2ZpbGUiLCJjbGllbnRIb3N0IjoiOTAuOTAuMTkzLjIwMiIsImNsaWVudElkIjoic2dzT2lnbmllcyIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwicHJlZmVycmVkX3VzZXJuYW1lIjoic2VydmljZS1hY2NvdW50LXNnc29pZ25pZXMiLCJjbGllbnRBZGRyZXNzIjoiOTAuOTAuMTkzLjIwMiJ9.TZ_lInL0F-nnLrZERA0-shD--brmMD7sJyBh6pHm8IISM-SetmqjkxuNDNXw4F9L8D52yww_OsGJf5mMVyYwA2-yZ2PX1EUAN6e69JmX3G4zl3u6RCEsBjHSYaeKARZ02M3_6PPjdRRCpmp_AMR99zKbUTTfcp3CK_XpwT4dU0mNtxZQZ6I8aOb7cR5lx2tP1wDHxkoHSdF1eXgpwUjkJF4zMmXLgD2j2G0xXTcLdNEIlrySeL-Y3BcHEpjMCPas1zLGZJm5Fy7FHQgN2nIoo-rxhYcex4cpRPl3DPHcy314cbapYzVcrCBQcyDHLUBx8iMUX1kLe3d_NjPeZ4_99Q
User-Agent: PostmanRuntime/7.29.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 6bcb500a-9d44-4772-bf4c-e87f3c039c4e
Host: aix.art-informatique.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 1531
Request Body
"xml": "<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Analysis laboratoryCode="Code fourni par U à chaque Prestataire." laboratoryHeadOfficeCode="Maison mère Laboratoire, code fourni par U" reportNumber="20412" sampleNumber="20412" beginDate="2022-02-23" endDate="2022-03-11" sampleDate="2022-02-11" sampleSite="Hyper U Vitry en Artois" zipCode="62490" vendorCode="Code Fournisseur" vendorSiteCode="ABSENCE" approvalNumber="N° d'agrément" ean="3256226757717" productCode="??" planificationCode="??" analysisType="ANAP" sampleType="PPC" batchNumber="3x (Lot: 2582 355.21 D1 13:47)" conclusionCode="C" specificationsRevision="Version 1" specificationsSignatureDate="2020-03-25" reportType="fullReport"><interpretation>Test Conclusion</interpretation><interpretationV2/><details><detail parameterCode="PARA001" accreditation="false" acceptanceMini="50" acceptanceMaxi="100" acceptanceText="75"><parameterName>Coton</parameterName><unit>%</unit><method>A4</method><result>1</result><uncertainty>0.02</uncertainty><conclusionCode>C</conclusionCode></detail></details></Analysis>"
 
Last edited:
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Dear @EnriqueGonzalez , I sent you a "coffee"
Thank you! ?

please try:
B4X:
    Dim body As String = $"xml=<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"$
    Dim j As HttpJob
    j.Initialize("",Me)
    j.PostString("https://aix.art-informatique.com/qualibase/rest/v1/analyse/integration",body)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Authorization","Bearer eyJhbGciOiJSUzUxMiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ1Mz")
    wait for (j) jobdone (j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release
 
Last edited:
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Thank you! ?

please try:
B4X:
    Dim body As String = $"xml=<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"$
    Dim j As HttpJob
    j.Initialize("",Me)
    j.PostString("https://aix.art-informatique.com/qualibase/rest/v1/analyse/integration",body)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Authorization","Bearer eyJhbGciOiJSUzUxMiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ1Mz")
    wait for (j) jobdone (j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release

no good, response is

B4X:
{"success":false,"errors":[{"errorMessage":"Xml est obligatoire"}]}

in English : Xml is mandatory

here is the doc:

1667939475247.png
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
According to the above specs the api expects a Json String with a property "xml" (can't see from the picture if it needs other params), so something like this:

B4X:
dim m as map = CreateMap("xml": $"<?xml version="1.0" encoding... ?>"$) '<--- full xml string as in the api specs
dim jsonG as JsonGenerator
jsonG.Initialize(m)
dim body as String = jsonG.ToString

' rest of the httpjob 
...

Alwaysbusy
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
hi Imbault! it seems that the api acceptsjson, if that is so then try

B4X:
    Dim m As Map
    m.Initialize
    m.Put("xml",$"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"$)
    
    Dim json As JSONGenerator
    json.Initialize(m)
    
    Dim j As HttpJob
    j.Initialize("",Me)
    j.PostString("https://aix.art-informatique.com/qualibase/rest/v1/analyse/integration",json.ToString)
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("Authorization","Bearer eyJhbGciOiJSUzUxMiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ1Mz")
    wait for (j) jobdone (j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release

or with url form encoded
B4X:
    Dim su As StringUtils
    Dim xml As String = su.EncodeUrl($"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"$,"UTF8")
    Dim body As String = $"xml=${xml}"$
    
    Dim j As HttpJob
    j.Initialize("",Me)
    j.PostString("https://aix.art-informatique.com/qualibase/rest/v1/analyse/integration",body)
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("Authorization","Bearer eyJhbGciOiJSUzUxMiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ1Mz")
    wait for (j) jobdone (j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release
 
Upvote 0
Top