0

Desired Result: Get the URL - https://secure.tdameritrade.com/authCafe?code=123456789....

I am trying to get the code from image#2 listed below. It is located in a redirect URL. I looked all over stackoverflow but none of the advice worked. I tried every variation. If someone can help, it would be greatly appreciated

URL: https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=https://secure.tdameritrade.com/authCafe&client_id=MOBI@AMER.OAUTHAP

Image #1: enter image description here

Image #2: enter image description here

Image #3: I keep getting this URL error: enter image description here

My Current Failing code:

import requests
import json

request_session = requests.session()
url = 'https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=https://secure.tdameritrade.com/authCafe&client_id=MOBI@AMER.OAUTHAP'
data = {'username':'MY_USERNAME', 'password': 'MY_PASSWORD'}
auth_reply = request_session.post(url, data=data)
#auth_reply = request_session.post(url, auth=('MY_USERNAME', 'MY_PASSWORD'))
#auth_reply2 = request_session.get(auth_reply.url)
#auth_reply = request_session.post(url, data=data)
print(auth_reply.history[0].url)
print(auth_reply.history)
print(request_session.get(auth_reply.url).url)
print(auth_reply.url)

I would get status code 302 or status code 200 depending on post/get

  • What is status code for `auth_reply`? I assume it is 30x, and you should be able to find the target url in the `auth_reply.headers`, probably under `["Location"]`. – mfrackowiak Jan 28 '19 at 16:29
  • neither 302 nor 200 are error status codes. 200 is "success/ok" and 302 is ["redirect/found"](https://httpstatuses.com/302) – Felk Jan 28 '19 at 16:36
  • @mfrackowiak Depending on the variation sometimes i get status code 302 or status 200. But they always result in the URL with "?error=access_denied..." instead of the code URL i want. I tried the headers one but nothing of what I am looking for – the_begging_beginner Jan 28 '19 at 16:37

1 Answers1

0

I found the solution. The website is not optimized for simple username/password authentication. In order to sign in programmatically, I would also have to input the hidden values into the:

data = {'username':'MY_USERNAME', 'password': 'MY_PASSWORD', '_csrf' : ...}

The remaining hidden values can be generated by their JS files.

This is a scraping approach to the problem.

Image #1: enter image description here