Geting Http 400 Error on log in to Google API

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
DaleSigwart
Posts: 6
Joined: Thu Dec 07, 2023 10:23 pm

Geting Http 400 Error on log in to Google API

Post by DaleSigwart »

Hi,

I am working on new project that requires a log into API and send parameters and API key in headers only

format is https://maps.googleapis.com/maps/api/ge ... ..&key=AIz...

This is my HTTPAPI routine

dcl-s response varchar(16000000);
dcl-s formdata varchar(1024);
dcl-s apiKey varChar(40);


http_setOption( 'debug-level' : '2');
http_debug(*on: '/Google/GEOCoding/logging/httplog' +
%trim(prcorid) + '.txt');

* set httpi codes
http_setauth(HTTP_AUTH_NONE: '':'');
http_SetOption('network-ccisd': '1208');

* Load API key from TxoApiCfg
apikey = %trim(TXCLENTSEC);

URL = 'https://maps.googleapis.com/maps/api/geocode/json?';

rc = http_xproc(HTTP_POINT_ADDL_HEADER:%paddr(add_Addr_headers));

rc = http_req( 'POST'
: %trim(URL)
: *omit
: Response
: *omit
: *omit
: 'application/json');

// HTTP failures and communication failures
if rc <> 1;
msg = http_error();
dsply msg;
endif;

rc= https_cleanup();

//-------------------------------------------------------------------------------
// add_Addr_Headers -- Subprocedure
//-------------------------------------------------------------------------------
dcl-proc add_Addr_Headers;
dcl-pi *n;
headers varchar(32767);
end-pi;
headers =
'address=' + %trim(formdata) +
'&key=' + %trim(apiKey) + crlf;
end-proc;

debug log -- Note identifying information was Masked
HTTPAPI Ver 1.45 released 2021-09-20
NTLM Ver 1.4.0 released 2014-12-22
OS/400 Ver V7R4M0

2023-12-28-10.36.36.426552: http_setauth(): entered
2023-12-28-10.36.36.430042: http_persist_open(): entered
2023-12-28-10.36.36.430091: http_long_ParseURL(): entered
2023-12-28-10.36.36.430164: DNS resolver retrans: 2
2023-12-28-10.36.36.430179: DNS resolver retry : 2
2023-12-28-10.36.36.430192: DNS resolver options: x'00000136'
2023-12-28-10.36.36.430220: DNS default domain: xxx.COM
2023-12-28-10.36.36.430236: DNS server found: xxx.xx.5.11
2023-12-28-10.36.36.430249: DNS server found: xxx.xx.16.5.12
2023-12-28-10.36.36.430270: Resolving host maps.googleapis.com
2023-12-28-10.36.36.430292: inet_addr return value for this host is 4294967295
2023-12-28-10.36.36.430794: gethostbyname() returned 2902291295
2023-12-28-10.36.36.430826: Looking up service https
2023-12-28-10.36.36.430851: Service table returns port 443
2023-12-28-10.36.36.430895: Nagle's algorithm (TCP_NODELAY) disabled.
2023-12-28-10.36.36.452378: SNI hostname set to: maps.googleapis.com
2023-12-28-10.36.36.501963: -------------------------------------------------------------------------------------
2023-12-28-10.36.36.502000: Dump of server-side certificate information:
2023-12-28-10.36.36.502012: -------------------------------------------------------------------------------------
2023-12-28-10.36.36.502027: Cert Validation Code = 6000
2023-12-28-10.36.36.514946: -----BEGIN CERTIFICATE-----
MIIGEjCCBPqgAwIBAgIQDW4VistptnES0TZTlfkMYzANBgkqhkiG9w0BAQsFADBG...
pdEptrqDy1vgw4xsZcCCJyGwMI+LgQ==
2023-12-28-10.36.36.515155: -----END CERTIFICATE-----
Serial Number: 0D:6E:15:8A:CB:69:B6:71:12:D1:36:53:95:F9:0C:63
Common Name: upload.video.google.com
Issuer CN: GTS CA 1C3
Issuer Country: US
Issuer Org: Google Trust Services LLC
Version: 3
not before: 20231120020850
Unknown Field: 02:08:50 20-11-2023
not after: 20240212020849
Unknown Field: 02:08:49 12-02-2024
pub key alg: 1.2.840.10045.2.1
signature algorithm: 1.2.840.113549.1.1.11
Unknown Field: 03420004555F1E892BA7275CB78A5DDC64CFEBC8F223D2913E2DEC1061392FD64C3F009E14198F49197EFA84B7D6C56394B3E844662979D59A4640BA30C8FC07FAA16E3E
Unknown Field: 256
Unknown Field: DCBFEE0DD07DBA79D0E1F5B47D460E3F
Unknown Field: 1.2.840.113549.2.5
Unknown Field: 10D0ED9AF453C899DEB65E5C04E6200B687D46EC
Unknown Field: BC5E71C15AA5DD67BFED14DB1C4EF28E5EBED79AF91F7A64C73C9BED83B28C95
Unknown Field: 1
Unknown Field: upload.video.google.com
Unknown Field: 0
Unknown Field: 1.3.6.1.5.5.7.3.1
Unknown Field: 1.3.6.1.4.1.11129.2.5.3
Unknown Field: 2.23.140.1.2.1
Unknown Field: http://ocsp.pki.goog/gts1c3

2023-12-28-10.36.36.515939: Protocol Used: TLS Version 1.2
2023-12-28-10.36.36.515964: http_persist_req(POST) entered.
2023-12-28-10.36.36.515991: http_long_ParseURL(): entered
2023-12-28-10.36.36.516031: http_long_ParseURL(): entered
2023-12-28-10.36.36.516058: do_oper(POST): entered
2023-12-28-10.36.36.516115: There are 0 cookies in the cache
2023-12-28-10.36.36.516149: CommSSL_BlockWrite(): gsk_secure_soc_write socket fd=1, flags=00000084, blocking=0
2023-12-28-10.36.36.516203: CommSSL_BlockWrite(): gsk_secure_soc_write rc=0, len=93
POST /maps/api/geocode/json? HTTP/1.1
Host: maps.googleapis.com
User-Agent: http-api/1.45

2023-12-28-10.36.36.516226: CommSSL_BlockWrite(): gsk_secure_soc_write socket fd=1, flags=00000084, blocking=0
2023-12-28-10.36.36.516253: CommSSL_BlockWrite(): gsk_secure_soc_write rc=0, len=93
address=506 ***************&key=AIz*******************E

2023-12-28-10.36.36.516275: CommSSL_BlockWrite(): gsk_secure_soc_write socket fd=1, flags=00000084, blocking=0
2023-12-28-10.36.36.516300: CommSSL_BlockWrite(): gsk_secure_soc_write rc=0, len=2


2023-12-28-10.36.36.516317: recvresp(): entered
2023-12-28-10.36.36.516332: recvresp: reading response header, space left=32767
HTTP/1.0 400 Bad Request

2023-12-28-10.36.36.638525: recvresp: reading response header, space left=32741
Content-Type: text/html; charset=UTF-8

2023-12-28-10.36.36.638560: recvresp: reading response header, space left=32701
Referrer-Policy: no-referrer

2023-12-28-10.36.36.638577: recvresp: reading response header, space left=32671
Content-Length: 1555

2023-12-28-10.36.36.638593: recvresp: reading response header, space left=32649
Date: Thu, 28 Dec 2023 16:36:36 GMT

2023-12-28-10.36.36.638610: recvresp: reading response header, space left=32612


2023-12-28-10.36.36.638627: recvresp: empty line, ending header, number of eol chars=2
2023-12-28-10.36.36.638646: recvresp: header resp code = 400 repeating=0
2023-12-28-10.36.36.638662: SetError() #13: HTTP/1.0 400 Bad Request
2023-12-28-10.36.36.638675: recvresp(): end with 400
2023-12-28-10.36.36.638700: recvdoc parms: identity 1555
2023-12-28-10.36.36.638741: header_load_cookies() entered
2023-12-28-10.36.36.638778: recvdoc(): entered
2023-12-28-10.36.36.638791: SetError() #0:
2023-12-28-10.36.36.638805: recvdoc(): Receiving 1555 bytes.
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 400 (Bad Request)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelo ... 0x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelo ... 0x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelo ... 0x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/br
2023-12-28-10.36.36.638828: recvdoc(): have 1182 of 1555
anding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>400.</b> <ins>That’s an error.</ins>
<p>Your client has issued a malformed or illegal request. <ins>That’s all we know.</ins>

2023-12-28-10.36.36.638869: recvdoc(): have 1555 of 1555
2023-12-28-10.36.36.638883: SetError() #13: HTTP/1.0 400 Bad Request
2023-12-28-10.36.36.638915: http_close(): entered


Any help Scott from you or the community is very much appreciated

Thank you,
Dale
stefan@tageson.se
Posts: 12
Joined: Wed Jul 28, 2021 7:55 am
Location: Viken, Sweden
Contact:

Re: Geting Http 400 Error on log in to Google API

Post by stefan@tageson.se »

Community replying :)
Looking briefly at the api documentation - it says you can test this in your browser....
That seems to imply that you eventually should use a GET instead of a POST requsest.

All the best,
Stefan
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: Geting Http 400 Error on log in to Google API

Post by Scott Klement »

DaleSigwart wrote: Thu Dec 28, 2023 5:19 pm format is https://maps.googleapis.com/maps/api/ge ... ..&key=AIz...
I'm a little confused, because you gave an explanation that said it has to be passed as a header. Then you show the above, which is the URL, not a header... Are you trying to add query string data to the URL?

DaleSigwart wrote: Thu Dec 28, 2023 5:19 pm //-------------------------------------------------------------------------------
// add_Addr_Headers -- Subprocedure
//-------------------------------------------------------------------------------
dcl-proc add_Addr_Headers;
dcl-pi *n;
headers varchar(32767);
end-pi;
headers =
'address=' + %trim(formdata) +
'&key=' + %trim(apiKey) + crlf;
end-proc;
This will successfully add data to the headers, and indeed, I see that data in your trace file. (It was really hard to find because you've got all sorts of extra logging turned on that makes the log more difficult to read, but... it's there.)

However, the string you've provided here is NOT a valid header string. And the exampe isn't for a header, it's for query-string data in the URL. So why are you putting it in the headers?

You went through all this extra work to create an xproc, add it to the call, etc... if your data is part of the URL, just add it to the URL!

Code: Select all

  URL = 'https://maps.googleapis.com/maps/api/geocode/json'
      + '?address=' + %trim(formdata) 
      + '&key=' + %trim(apiKey) + crlf;
Also, I'm wondering if the data in 'formdata' and 'apiKey' is already URL-safe? If not, you'll need to encode it:

Code: Select all

  URL = 'https://maps.googleapis.com/maps/api/geocode/json'
      + '?address=' + http_urlEncode(%trim(formdata))
      + '&key=' + http_urlEncode(%trim(apiKey)) + crlf;
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: Geting Http 400 Error on log in to Google API

Post by Scott Klement »

Also, if you have to send another trace file, please remove this line of code before generating it:

Code: Select all

http_setOption( 'debug-level' : '2');   // <--- REMOVE THIS!!
This should only be included in very specific circumstances. It makes the log much harder for me to read. If I wanted all this extra detail all of the time, I would've made it the default.
DaleSigwart
Posts: 6
Joined: Thu Dec 07, 2023 10:23 pm

Re: Geting Http 400 Error on log in to Google API

Post by DaleSigwart »

Hi Scott,

Thanks again for your help.

Once I changed the code add the request and the key to the URL header it worked fine.

Dale
Post Reply