OUATH2

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: OUATH2

Post by Scott Klement »

the code you provided (http_setoption) is how you change the timeout.

This doesn't affect HTTP_TIMEOUT, which is just a constant.

Please explain why you are asking this. Are you having a problem? What problem are you having?
Bruceg
Posts: 19
Joined: Fri Apr 01, 2022 2:03 pm

Re: OUATH2

Post by Bruceg »

I am trying to use a modified version of your example 14, getting tracking information but using the OAUTH2 token . I have been working with UPS to get this working correctly but, they have not been a lot of help. The connection closes too quickly when I am in debug so I would like to extend the time out .

I have also sent you what I see in the debug log along with part of the code.

Thanks

http_xproc( HTTP_POINT_ADDL_HEADER: %paddr(AddHeader) );

postData =
'<?xml version="1.0"?>'
'<TrackRequest xml:lang="en-US">'
'<Request>'
'<TransactionReference>'
'<CustomerContext>'
'TRACKING TEST'
'</CustomerContext>'
'</TransactionReference>'
'<RequestAction>Track</RequestAction>'
'<RequestOption>activity</RequestOption>'
'</Request>'
'<TrackingNumber>' + TrackingNo + '</TrackingNumber>'
'</TrackRequest>'

rc =
http_url_post_xml('https://onlinetools.ups.com/api/ups.app/xml/Track'
: %addr(postData) + 2
: %len(postData)
: %paddr(StartOfElement)
: %paddr(EndOfElement)
: *NULL) ;
dcl-proc AddHeader ;
dcl-pi *n;
headers varchar(32767);
end-pi;

Headers = 'Authorization: Bearer' + ' ' +
a_token + CRLF + ' ' +
'Content-type : application/xml' + CRLF;
END-PROC addHeader ;


Log :
SetError() #13: HTTP/1.1 200 OK
recvresp(): end with 200
recvdoc parms: identity 328
header_load_cookies() entered
recvdoc(): entered
SetError() #0:
recvdoc(): Receiving 328 bytes.
<?xml version="1.0"?><TrackResponse><Response><ResponseStatusCode>0</ResponseStatusCode><ResponseStatusDescription>Failure</ResponseStatusDescription><Error><ErrorSeverity>Hard</ErrorSeverity><ErrorCode>250002</ErrorCode><ErrorDescription>Invalid Authentication Information.</ErrorDescription></Error></Response></TrackResponse>
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: OUATH2

Post by Scott Klement »

Changing HTTPAPI's timeout won't prevent the server from timing out, it'll only prevent HTTPAPI from timing out. You are using the old http_url_post_xml() routine, which has a parameter for the timeout.. you can just pass that parameter if you want to change HTTPAPI's timeout.)

Also, not sure if it's just the way you posted it or not, but this code looks wrong:

Code: Select all

Headers = 'Authorization: Bearer' +  ' '  +       
   a_token + CRLF + ' ' +                            
   'Content-type : application/xml' + CRLF;          
Two problems:
  • This will result in a duplicate Content-Type. HTTP requires the content-type when a document is sent, so HTTPAPI is going to send one, and you are manually sending a second one.
  • There is a weird extra space (' ') before the Content-Type
Please consider eliminating this nastiness and using HTTP_setauth instead unless there's a good reason to manually code your own headers.

Code: Select all

 http_setAuth( HTTP_AUTH_BEARER: '': a_token);

 rc = http_url_post_xml( 'https://onlinetools.ups.com/api/ups.app/xml/Track'
                      : %addr(postData: *data) 
                      : %len(postData)
                      : %paddr(StartOfElement)
                      : %paddr(EndOfElement)
                      : *NULL
                      : 30        <-- your time out value
                      : *omit
                      : 'application/xml') ;
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: OUATH2

Post by Scott Klement »

Also verify that 'atoken' is large enough for the entirety of the token. If it is larger than the 15000 characters allowed by http_setAuth, then consider using the xproc technique.
Bruceg
Posts: 19
Joined: Fri Apr 01, 2022 2:03 pm

Re: OUATH2

Post by Bruceg »

thank you for your help
Last edited by Bruceg on Fri May 31, 2024 2:37 pm, edited 1 time in total.
Bruceg
Posts: 19
Joined: Fri Apr 01, 2022 2:03 pm

Re: OUATH2

Post by Bruceg »

Scott,
I used your code and I was successful . thanks for your patience and assistance on this. If anyone else needs help on using OAUTH2 to use the UPS API's i am more then happy to help

Bruce
Dpevolutionuk10
Posts: 2
Joined: Tue Nov 26, 2024 2:05 pm

Re: OUATH2

Post by Dpevolutionuk10 »

Hi Bruce,

Your replies to the questions are great. With your offer of help would you consider putting a code sample on the thread to show us who are new to working with OAUTH2 how you managed to get the process to work?
I need something similar, been given a URL which I need to use to get the token before using the HTTP_STRING(POST.... to process the actual data etc.

Your help if you have time would be appreciated....
emaxt6
Posts: 18
Joined: Mon Jun 05, 2023 4:02 pm

Re: OUATH2

Post by emaxt6 »

Nowadays is better - as UPS team said to me - to directly go the JSON / HTTP API route version of the interface, and bypass XML, that is considered in some sense legacy.
I would suggest to avoid building datastructure "by hand", like string concatenation, use the tools to proper serialize/deserialize, a lot of time can be lost in those details, that are problems already solved using parser and serializer (DATA-GEN DATA-INTO).

For UPS OAUTH2 I would suggest start with a proper DS (that is the passed to various programs, implementing like "tracking", "shipping" macro operations). Build a UPS_H header that includes all the needed DSs.
In the return DSs just place the info you need, as a start.
For the token I use something like the following, where "local_" vars are not UPS ones but are added for convenience of the downstream programs to give the token and some context env info.

Just use DATA-GEN or DATA-INTO , and avoid any "manual" concatenation stuff.

Code: Select all

DCL-DS UPSAuthToken_t TEMPLATE;
  token_type VARCHAR(80);
  issued_at VARCHAR(80);
  client_id VARCHAR(80);
  access_token VARCHAR(3000);
  scope VARCHAR(80);
  expires_in VARCHAR(80);    
  refresh_count VARCHAR(80); 
  status VARCHAR(80);
  local_expiresAfter TIMESTAMP; //UTC
  local_issued_at TIMESTAMP;
  local_expiresAfterSysTime TIMESTAMP; //SYS
  local_nOfUse INT(10);
  local_environment CHAR(10); 
  local_baseURL VARCHAR(50); // will change according to environment, used in downstream programs
END-DS;
Post Reply