Getting access token with Python

Hi all, I am trying to get an access token using Python but i kept receiving the following errors when i try to print self.token.

Code snippet 1:

tokenResponse = requests.post("https://developers.onemap.sg/privateapi/auth/post/getToken",
 data={"email": email, "password": password},
 headers={'cache-control': 'no-cache',
 'content-type': 'multipart/form-data;'
 ' boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'})

self.token = tokenResponse.json()

Error message 1:
{‘code’: ‘BadRequestError’, ‘message’: ‘MultipartParser.end(): stream ended unexpectedly: state = START_BOUNDARY’}

Code snippet 2:

tokenResponse = requests.post("https://developers.onemap.sg/privateapi/auth/post/getToken",
 data={"email": email, "password": password})

self.token = tokenResponse.json()

Error message 2:
{‘error’: ‘You have to pass in both email and password to retrieve your token.’}

Any advice will be appreciated!
Thank you!

Hi Jem,

This is the sample code to request for token using python.

Regards,
Kai

I tried the code but still got an error message

import http.client

conn = http.client.HTTPSConnection("developers.onemap.sg")
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=sampleEmail\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=samplePassword\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW–".encode(
 "UTF-8")

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()

print(data.decode("utf-8"))

Error message:
{“code”:“BadRequestError”,“message”:“parser error, 115 of 227 bytes parsed”}

Apologies as I am still new to this and thank you for your help!

payload = “------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="email”\r\n\r\nsample email\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=“password”\r\n\r\ndemo123!\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW–”.encode( “UTF-8”)

Hi Jem,

This is because your parameter names are missing. :slight_smile:

@yitingg We have updated the topic thread posting spanlife to 3 months (2190 hours) instead of 1 year (8160 hours) after the last thread posted.

Therefore, the topic thread will be closed after 3 months if there’s no activity in the thread.

Hi,

I like to check that you are supposed to replace “email” and “password” with your own? What do you do with the quotes (" ")?

Thank you.

Hi Marcus,

You will just need to replace your email and password into the payload.

The quotes, you can just leave it there. It will look something like below.

payload = “------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="email”\r\n\r\ntest@onemap.sg\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=“password”\r\n\r\nmypassword\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW–”.encode( “UTF-8”)