REST API to get authorization token

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
panders400
Posts: 7
Joined: Thu Jul 20, 2023 8:11 pm

REST API to get authorization token

Post by panders400 »

I am very new to the HTTP API world, but have been tasked with getting these APIs for Tradebeyond/CBX to work.

I have the oauth/token API working in Postman, but can't seem to get it right in the RPG. I am looking for any help I can get.

The url is https://partycity-sandbox-api.cbxcloud.com/oauth/token.

In Postman it shows the HTTP code snippet as:

Code: Select all

POST /oauth/token HTTP/1.1
Host: partycity-sandbox.cbxcloud.com
Content-Type: application/x-www-form-urlencoded
Authorization: Basic Y2J4OmNieEAxMjM=
Content-Length: 78

username=<our userid>&password=<our pwd>&grant_type=password
My code I have tried is:

Code: Select all

          Request = 'username='+ http_urlencode(Userid)
                  + '&password='+http_urlencode(Pwd)
                  +'&grant_type=password';

          URL = BaseURL + 'oauth/token';

          rc = http_xproc(HTTP_POINT_ADDL_HEADER:%paddr(add_Auth_headers));
          rc = http_req( 'POST'
                       : URL
                       : *omit
                       : response
                       : *omit
                       : request   );

          //             : 'application/x-www-form-urlencoded');
          if rc <> 1;
             msg = http_error();
             dsply msg;
          endif;                              
       //-------------------------------------------------------------------------------
       dcl-proc add_Auth_headers;
       dcl-pi *n;
         headers varchar(32767);
       end-pi;
          headers = 'Host: partycity-sandbox.cbxcloud.com' + crlf  +
                   'Content-Type: application/x-www-form-urlencoded' + crlf  +
                   'Authorization: Basic Y2J4OmNieEAxMjM='  + crlf  +
                   'Content-Length: ' + %Char(%Len(%Trim(request)))  + crlf;
          Return;
       end-proc;                
And I get: HTTP/1.1 400 Bad Request

I have also tried:

Code: Select all

          Form = WEBFORM_open();
          WEBFORM_SetVar(Form: 'username': userid);
          WEBFORM_SetVar(Form: 'password': pwd);
          WEBFORM_SetVar(Form: 'grant_type': 'password');
          WEBFORM_postData( Form : myPointer: dataSize );
          rc = http_url_post( URL
                            : myPointer
                            : dataSize
                            : '/edi/testurlpost.html'
                            : HTTP_TIMEOUT
                            : HTTP_USERAGENT
                            : 'application/x-www-form-urlencoded' );

          if rc <> 1;
             msg = http_error();
             dsply msg;
          endif;
          // When done, make sure you call this function to free up
          // the memory that the web form used
          //
          WEBFORM_close(Form);  
This results in CommTCP_read: Socket has been shut down.

I have tried setting HTTP_SetOption, HTTP_SetAuth, HTTP_POST, HTTP_STring and various combinations.
Any input would be greatly appreciated. I can't get by this first step of just getting the token and project has a date in August!
jonboy49
Posts: 206
Joined: Wed Jul 28, 2021 8:18 pm

Re: REST API to get authorization token

Post by jonboy49 »

There may be other issues but in the call to HTTP_POINT_ADDL_HEADER you are not just adding additional headers, you are including base ones like Host: and Content-Type that HTTPAPI is already adding. If you check the debug log you can confirm this.
panders400
Posts: 7
Joined: Thu Jul 20, 2023 8:11 pm

Re: REST API to get authorization token

Post by panders400 »

Thank you for your response, Jonboy. I made adjustments. It did change things. With 2 additional small changes (fixing the host and trimming my user id and password) IT WORKED!!!!!

Thank you!
Post Reply