Login example in Python

Sample Python code for login. Change the username and password to your test user.

import time
import base64
from M2Crypto import RSA
import httplib, urllib
import json

def print_json(j,prefix=''):
    for key, value in j.items():
        if isinstance (value,dict):
            print '%s%s' % (prefix,key)
            print_json(value, prefix+'  ')
        else:
            print '%s%s:%s' % (prefix,key,value)

username = 'username'
password = 'password'
service  = 'NEXTAPI'
URL='api.test.nordnet.se'
API_VERSION='2'

timestamp = int(round(time.time()*1000))
timestamp = str(timestamp)
buf = base64.b64encode(username)+':'+base64.b64encode(password)+':'+base64.b64encode(timestamp)
rsa=RSA.load_pub_key('NEXTAPI_TEST_public.pem')
encrypted_hash = rsa.public_encrypt(buf, RSA.pkcs1_padding)
hash = base64.b64encode(encrypted_hash)

headers = {"Accept": "application/json"}
conn = httplib.HTTPSConnection(URL)

# GET server status
conn.request('GET','/next/'+API_VERSION + '/', '', headers)
r=conn.getresponse()
response=r.read()
j = json.loads(response)
print_json(j)

# POST login
params = urllib.urlencode({'service': 'NEXTAPI', 'auth': hash})
conn.request('POST','/next/'+API_VERSION+'/login',params,headers)
r=conn.getresponse()
response=r.read()
j = json.loads(response)
print_json(j)

Another Python 3 example. Note: This code example is contributed by users to the forum, not provided by Nordnet (https://api.test.nordnet.se/boards/2/topics/2544)

#!/usr/bin/env python3
import time
import base64
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import http.client
import json
from urllib.parse import urlencode, quote_plus

def print_json(j, prefix=''):
    for key, value in j.items():
        if isinstance(value, dict):
            print('%s%s' % (prefix, key))
            print_json(value, prefix + '  ')
        else:
            print('%s%s:%s' % (prefix, key, value))

USERNAME = 'username'
PASSWORD = 'password'
SERVICE = 'NEXTAPI'
URL = 'api.test.nordnet.se'
API_VERSION = '2'

def get_hash(username, password):
    timestamp = int(round(time.time() * 1000))
    timestamp = str(timestamp).encode('ascii')

    username_b64 = base64.b64encode(username.encode('ascii'))
    password_b64 = base64.b64encode(password.encode('ascii'))
    timestamp_b64 = base64.b64encode(timestamp)

    auth_val = username_b64 + b':' + password_b64 + b':' + timestamp_b64
    rsa_key = RSA.importKey(open('NEXTAPI_TEST_public.pem').read())
    cipher_rsa = PKCS1_v1_5.new(rsa_key)
    encrypted_hash = cipher_rsa.encrypt(auth_val)
    encoded_hash = base64.b64encode(encrypted_hash)

    print(auth_val, encoded_hash)
    return encoded_hash

def main():
    auth_hash = get_hash(USERNAME, PASSWORD)

    headers = {"Accept": "application/json"}
    conn = http.client.HTTPSConnection(URL)

    # GET server status
    conn.request('GET', '/next/' + API_VERSION + '/', '', headers)
    r = conn.getresponse()
    response = r.read().decode('utf-8')
    j = json.loads(response)
    print_json(j)

    # POST login
    params = urlencode({'service': 'NEXTAPI', 'auth': auth_hash})
    conn.request('POST', '/next/' + API_VERSION + '/login', params, headers)
    r = conn.getresponse()
    response = r.read().decode('utf-8')
    j = json.loads(response)
    print_json(j)

if __name__ == "__main__":
    main()