TD Ameritrade - The API key in request is either null or blank or invalid

To get an oauth access token or refresh token from the TD Ameritrade API in Python, you may try to use normal query parameters like the following:

r = requests.post(
    f'https://api.tdameritrade.com/v1/oauth2/token?grant_type=refresh_token&refresh_token={refresh_token}&access_type=&code=&client_id=MYAPIKEY@AMER.OAUTHAP&redirect_url=http://localhost',
    }
)

However, this will result in the following error:

{'error': 'The API key in request is either null or blank or invalid.'}

Instead of using query parameters, use the data argument, which will submit it as x-www-form-urlencoded form data:

r = requests.post(
    'https://api.tdameritrade.com/v1/oauth2/token',
    data={
        'grant_type': 'refresh_token',
        'refresh_token': refresh_token,
        'client_id': 'MYAPIKEY@AMER.OAUTHAP',
    }
)

Comments

Add Comment

Name

Email

Comment

Are you human? - three = -1


Name: two lakes

Creation Date: 2021-06-18

Thanks for this! Was running out of things to try.


Name: LeLe

Creation Date: 2022-04-21

thank you!


Name: Zathereth

Creation Date: 2022-05-05

Oh my god, I have been searching all over the place all day for this. What are people even doing making programs that open up the web page in order to get their authorization codes? Like whole complex programs to open chrome and press the allow button when you can just do this. Anyway, ignoring my rant, thank you very very much for this.


Name: Michael Lewis

Creation Date: 2022-05-13

How do you obtain the client id ?


Name: Adam Perez

Creation Date: 2022-12-03

I still get an error, I am using AXIOS in my javascript version of the above. I am able get back the access_token and refresh_token when I put the data in Postman, but when I try to hit do this with my axios call I get the following error. I have tried everything. Would love some help! data: { error: 'The API key in request is either null or blank or invalid.' }