Skip to content

Instantly share code, notes, and snippets.

@rkulow
Last active April 2, 2022 04:28
Show Gist options
  • Save rkulow/5917485a0a657b31a2b74dd06b15490b to your computer and use it in GitHub Desktop.
Save rkulow/5917485a0a657b31a2b74dd06b15490b to your computer and use it in GitHub Desktop.
Degiro automated login with 2FA
import requests
import hmac, base64, struct, hashlib, time
import json
# 2FA code generation from https://stackoverflow.com/questions/8529265/google-authenticator-implementation-in-python
def get_totp_token(secret):
key = base64.b32decode(secret, True)
msg = struct.pack(">Q", int(time.time())//30)
h = hmac.new(key, msg, hashlib.sha1).digest()
o = h[19] & 15
h = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000
return h
def login(username, password, secret):
data = {
"username": username,
"password": password,
"loginButtonUniversal": "",
"isRedirectToMobile": False,
"queryParams": {
"reason": "session_expired"
},
"oneTimePassword": get_totp_token(secret)
}
req = requests.post('https://trader.degiro.nl/login/secure/login/totp', json=data)
if req.status_code == 200:
body = json.loads(req.text)
return body["sessionId"]
else:
print(f"Login not successful, status code {req.status_code}.")
return ""
# Degiro username
username = "******"
# Degiro password
password = "******"
# Degiro 2FA secret
# -> enable 2FA, scan qr code and extract secret
secret = "********************************"
session_key = login(username, password, secret)
print(session_key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment