Login problem

Hello,
I am using following code to set up connection.

import requests
username = ‘xxx’
password = ‘xxx’
url = ‘https://api.smarket.com/v3/sessions
data = { “password”: password,
“remember”: “true”,
“username”: username,
}

page = requests.post(url, json=data)

the reply is
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.smarket.com', port=443): Max retries exceeded with url: /v3/sessions (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000027063E91780>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

I don’t know why. Do I need to apply the API user before this connection?
Thanks

Hi,

Your url is incorrect, it should be https://api.smarkets.com/v3/sessions/

João

Thanks. But I still got a 400 error. Do I need to apply the API user first?

You need to apply in order to place orders, not for logging in.

Can you share the error details?

“true” also looks wrong, as it’s a string and not a boolean

Hi, this is the modified code.

import requests
import sys
username = ‘email’
password = ‘password’
url1 = ‘https://api.smarkets.com/v3/sessions
data = { “password”: password,
“remember”: 1,
“username”: username,
}

def R(text, end=’\n’): print(’\033[0;31m{}\033[0m’.format(text), end=end)
def G(text, end=’\n’): print(’\033[0;32m{}\033[0m’.format(text), end=end)

page = requests.post(url1, data=data)

if page.status_code != 200:
R(‘ERROR code:{}’.format(page.status_code))
#sys.exit()
else:
G(‘OK!’)

Here’s your code with some fixes:

import requests
import sys
username = 'email'
password = 'password'
url1 = 'https://api.smarkets.com/v3/sessions'
data = { 
    "password": password,
    "remember": True,
    "username": username,
}

def R(text, end='\n'): print('\033[0;31m{}\033[0m'.format(text), end=end)
def G(text, end='\n'): print('\033[0;32m{}\033[0m'.format(text), end=end)

page = requests.post(url1, json=data)

if page.status_code != 201:
    R('ERROR code:{}'.format(page.status_code))
#sys.exit()
else:
    G('OK!')

Hi Joao

I am trying to log into the API for the first time. I have a fair bit of coding experience but keep getting the same error, below.

To further test, I have tried the above code in order to attempt logging in to the API.

I have literally copied and pasted the code, changing the username and password variables to my own.

I receive the following error: HTTP Error 405: METHOD NOT ALLOWED

Any advice would be very helpful.

Hi Daniel,

The code above has two minor issues, the url should end with a forward slash and it should include the header Content-Type: application/json. Could you please try the following code:

import requests

headers = {"Content-Type": "application/json"}
data = {"username": "<username>", "password": "<password>"}
response = requests.post('https://api.smarkets.com/v3/sessions/', headers=headers, json=data)

Kind regards,
Isabel

Hi Isabel

Thank you for your reply.

I tried the code changing only the username and password to my own and got the same error message:

HTTP Error 405: METHOD NOT ALLOWED

I have only ever successfully logged in using the “Try it now” button at https://docs.smarkets.com/#/sessions/create_session

Any further advice would be gratefully appreciated.

Hi Daniel

Are you sure that you are using the post method?

Isabel

Hi Isabel

You have been extraordinarily helpful and most of all patient.

I am embarrassed to admit, I was using an unfamiliar IDE and had been running an erroneous python script over and over again. When I selected the correct script to run I received a 201 response.

Thanks again for your time.

Daniel.