Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http exception when getting access_token with refresh_token #23

Open
rcholic opened this issue Sep 8, 2022 · 0 comments
Open

http exception when getting access_token with refresh_token #23

rcholic opened this issue Sep 8, 2022 · 0 comments

Comments

@rcholic
Copy link

rcholic commented Sep 8, 2022

I put some logging statements in the credentials.py file to see what's going on:

Access Token Expired, refreshing access token...
the data below comes from my td_credentials.json file, which is posted to TD API to get access_token

data:  {'client_id': 'CV72580QKIEV4MOLPDGFXMDJC1YLSJ7I', 'grant_type': 'access_token', 'access_type': 'offline', 'refresh_token': 'redacted-----HkvASvG100MQuG4LYrgoVi/JHHvlO8IGgyu3fLRYaZKUDDce+CwdqwI7pVI8NW2owQFhIVg1sdpqQuZpuZtAeToZ63bxrELM5TEgpF/C6BoQNY2T0pkn22zgJnVduGAJvgzHOzybjzBasHkfO0d37ppqC0Cdr9urd4a4Rycp195EJLBpc8k3TnCkQ3d3dq9SjCoIdPvMVemeOcAbFmDZgM0KhDQtjJOuJA4QBvYBnhyB3FKDPlm3OnvajRTEHxzvNswfEWiZ/yWbZzK+hdO3ZWCwsM/70cyZ1phM0mE6Hat/oQYTj2M8VcDsrKxyWyFX4swgAQYDGgvCw5L'}

I got response 400 meaning the request is a bad request
response: <Response [400]>

Traceback (most recent call last):
  File "/Users/user1/temp/v2/test8.py", line 4, in <module>
    tos_client = TOSClient('my-app-consumer-key', '/Users/user1/temp/v2/project/tokens.json')
  File "/Users/user1/temp/v2/project/server/toolbox/TOSClient.py", line 23, in __init__
    td_credentials = TdCredentials(
  File "/Users/user1/env/lib/python3.9/site-packages/td/credentials.py", line 62, in __init__
    self.from_credential_file(file_path=credential_file)
  File "/Users/user1/env/lib/python3.9/site-packages/td/credentials.py", line 377, in from_credential_file
    self.from_token_dict(token_dict=token_dict)
  File "/Users/user1/env/lib/python3.9/site-packages/td/credentials.py", line 248, in from_token_dict
    self.validate_token()
  File "/Users/user1/env/lib/python3.9/site-packages/td/credentials.py", line 580, in validate_token
    token_dict = self.grab_access_token()
  File "/Users/user1/env/lib/python3.9/site-packages/td/credentials.py", line 560, in grab_access_token
    raise requests.HTTPError()
requests.exceptions.HTTPError

I installed this library using pip install btw. Is there any known issue?

this below method in credentials.py throws the exception above:

    def grab_access_token(self) -> dict:
        """Refreshes the current access token.

        This takes a valid refresh token and refreshes
        an expired access token. This is different from
        exchanging a code for an access token.

        ### Returns
        ----
        dict:
            The dictionary contain all the token
            info.
        """

        # build the parameters of our request
        data = {
            'client_id': self.client_id,
            'grant_type': 'refresh_token',
            'access_type': 'offline',
            'refresh_token': self.refresh_token
        }

        # Make the request.
        response = requests.post(
            url="https://api.tdameritrade.com/v1/oauth2/token",
            headers={
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            data=data
        )

        if response.ok:
            return response.json()
        print('data: ', data)
        print('response: ', response)

        raise requests.HTTPError()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant