studerw / td-ameritrade-client

TD Ameritrade Java Client
Apache License 2.0
69 stars 47 forks source link

"error": "invalid_grant" #23

Closed jschlade closed 4 years ago

jschlade commented 4 years ago

Hi,

Please help me. I would like to use your API but I'm having trouble.

Followed this: https://developer.tdameritrade.com/content/simple-auth-local-apps

At this point: https://developer.tdameritrade.com/authentication/apis/post/token-0

The request looks like:

grant_type=authorization_code&refresh_token=&access_type=offline&code=MIbbl7ok....&client_id=OAXV....0XX&redirect_uri=http%3A%2F%2Flocalhost

but I get the response:

HTTP/1.1 400 Bad Request . . { "error": "invalid_grant" }

Any idea why?

Following these detailed instructions. Seems very simple.

https://www.reddit.com/r/algotrading/comments/914q22/successful_access_to_td_ameritrade_api/

I'm so frustrated and any help is appreciated.

Thank you

studerw commented 4 years ago

I use Docker and curl to do this. Here are my notes:

How I did this (April 2020):

docker run --name mynginx1 -p 80:80 -d nginx

  1. 1st request - do in the browser. It will do a bad redirect, but copy the code you get back. You should open up Chrome Dev tools or similar for Firefox or Safari to see the redirect in the Network Tools.

https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1&client_id=DJZF...BKNH19N%40AMER.OAUTHAP https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1&client_id=DJZF...19N%40AMER.OAUTHAP

  1. Using Curl Post with NGINX running a local server on 127.0.0.1

curl -X POST --header "Content-Type: application/x-www-form-urlencoded" -d "grant_type=authorization_code&refresh_token=&access_type=offline&code=&client_id=DJZF...BKNH19N&redirect_uri=http%3A%2F%2F127.0.0.1" "https://api.tdameritrade.com/v1/oauth2/token"

Obviously, insert the URL encoded 'code' you get back in step one into the spot in temp 2. I just use some online URL decoder site to do the parameter encoding for that.

The docker running on localhost will take the redirect call from step 2 and allow you to see the refresh token you get back. Again, remember when using the TDA Java API with your new refresh token, you don't use the @AMER.OAUTHAP in the client_id.

Let me know if that helps.

Bill

On Tue, Jul 28, 2020, at 04:13, jschlade wrote:

Hi,

Please help me. I would like to use your API but I'm having trouble.

Followed this: https://developer.tdameritrade.com/content/simple-auth-local-apps

At this point: https://developer.tdameritrade.com/authentication/apis/post/token-0

The request looks like:

grant_type=authorization_code&refresh_token=&access_type=offline&code=MIbbl7ok....&client_id=OAXV....0XX&redirect_uri=http%3A%2F%2Flocalhost

but I get the response:

HTTP/1.1 400 Bad Request . . { "error": "invalid_grant" }

Any idea why?

Following these detailed instructions. Seems very simple.

https://www.reddit.com/r/algotrading/comments/914q22/successful_access_to_td_ameritrade_api/

I'm so frustrated and any help is appreciated.

Thank you

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/studerw/td-ameritrade-client/issues/23, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQHJA5TOX7WMG6DOMJFIU3R52JETANCNFSM4PKJZHYQ.

jschlade commented 4 years ago

Hi Bill,

Thank you for the reply. I was so frustrated yesterday but it turns out that I just needed to generate another code. I think when I was trying for the first time there was some other issue going on with the td ameritrade server. I guess that's just a good note to self if you constantly see a 400 bad request even if you're following the steps correctly.

The following day once I generated a new node code and used that one I was finally able to generate an access_token and refresh_token.

Given the initial access_token, refresh_token, scope, expires in, refresh_token_expires_in and token_type I wonder what it would take to store, reuse and regenerate them as needed?

Thanks again