Unable to retrieve access token using python

Hi Admin,

I’m a new python user, my python version is 3.6.5. I tried to retrieve the access token using Python based on the codes found in thread below.

When i tried to run the codes
import http.client

conn = http.client.HTTPSConnection(“developers.onemap.sg”)

payload = “------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=“email”\r\n\r\ndemo@onemap.sg\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=“password”\r\n\r\ndemo123!\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW–”

headers = {
‘content-type’: “multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW”,
‘cache-control’: “no-cache”

conn.request(“POST”, “/privateapi/auth/post/getToken”, payload, headers)

res = conn.getresponse()
data = res.read()


Python hit an error at line 3…
File “”, line 3
conn = http.client.HTTPSConnection(“developers.onemap.sg”)
SyntaxError: invalid character in identifier

Appreciate your guidance please.

Hi Sean,

How about trying out the request library?

Thanks for your prompt reply Kaicong and appreciate your patient.

Can i trouble you to share with me the request library link please? I tried browsing under categories but did not managed to locate any library there.

Aside to the above, i just tried running the codes you have provided but again received an error on line 3.
See below.

File “”, line 3
url = “https://developers.onemap.sg/privateapi/auth/post/getToken”
SyntaxError: invalid character in identifier

Hi Sean,

For a start, I will like to find out what operating system you are using. If you are using MacOS, the double quotes might be different and caused a syntax error.

requests library can be installed through pip

I’m on windows 7. =) Interesting.

I have finally managed to run the codes by retyping all " and replacing " with ’ for username and password. Not sure what happens to the characters when i copy and paste onto my jupyter workbook.

hmmm, connection error.

ConnectionError: HTTPSConnectionPool(host=‘developers.onemap.sg’, port=443): Max retries exceeded with url: /privateapi/auth/post/getToken (Caused by NewConnectionError(’<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000005F8FDD8>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond’,))

Hi Sean

Are you running your jupyter workbook on a virtual environment e.g. virtualbox. You may want to check if port 443 is open?

Ran into the same issues as Sean, and OP in

My solution:

no_file_multipart_req = requests.Request(‘POST’, “https://developers.onemap.sg/privateapi/auth/post/getToken”,
files={‘email’: (None, ‘myemail@email.com’),
‘password’: (None, ‘mypassword’)},

s = requests.Session()
resp = s.send(no_file_multipart_req)

Basically, Python requests doesn’t have explicit support for multiforms, you’ll have to use this method instead.
Also, there’s no need to, and we shouldn’t be setting the boundaries ourselves.

(credits to Franklin Gu https://franklingu.github.io/programming/2017/10/30/post-multipart-form-data-using-requests/, who incidentally appears to also be based in S’pore)

(please replace the quotes with straight quotes, I think this webform is messing with them;; admins, you might want to look into that)

1 Like

Hi Mark,

Great to learn from you on this!!

Thumbs up for community sharing.:grin: