Skip to content

Cannot request new token with auth code for the Digi-key API #556

@lpdx

Description

@lpdx

Issue / Problem report

  1. Add the KiCost version. Type kicost --version at terminal.

KiCost v1.1.18
kicost-digikey-api-v3 v0.1.3
Python 3.9.16 (KiCAD 7.0)
OS Windows 10 Enterprise, Zscaler running

The tool works well with the Mouser and Nexar API and the problem occurs when enabling Digi-Key API.

To isolate the problem, I created a fresh virtual env with Python 3.9.16 (same version as in KiCAD 7.0 installation) and installed just the kicost-digikey-api-v3.

Steps to reproduce:

  • Create a new python env conda create -n fresh python=3.9.16 and activate it
  • download, install and configure the kicost-digikey-api-v3 following the procedure here
  • enter the downloaded kicost-digikey-api-v3 folder and run the test with python test_production.py
  • I had an error about SSL: CERTIFICATE_VERIFY_FAILED so I install python -m pip install pip-system-certs to solve
  • run python test_production.py again
  • the browser opens and asks to allow access, click "Allow" with the two options marked ("OAUTH.APPROVAL.PAGE.TEMPLATE.DEFAULTSCOPEDESCRIPTION" and "DEFINES WHAT USERS CAN VIEW ABOUT THEIR OWN PROFILE")
  • The page is updated for the callback url https://localhost:8139/digikey_callback?code=XXXX&scope=
  • Next page shows that "You may now close this window, Auth code retreived: XXXX"
  • After closing the page the log in the prompt shows this messages:
(fresh-env) C:\Users\username\git\kicost-digikey-api-v3>python test_production.py
2024-01-25 17:19:01,445 - kicost_digikey_api_v -    DEBUG: Digi-Key API plug-in options:
2024-01-25 17:19:01,445 - kicost_digikey_api_v -    DEBUG: ['DIGIKEY_STORAGE_PATH=C:\\Users\\username\\.cache\\kicost\\Digi-Key', 'DIGIKEY_CLIENT_ID=XXXXXXXXNc84M', 'DIGIKEY_CLIENT_SECRET=XXXXXXXX7Ks3f', 'DIGIKEY_CLIENT_SANDBOX=True']
2024-01-25 17:19:01,445 - kicost_digikey_api_v -    DEBUG: {}
Looking in cache C:\Users\username\.cache\kicost\Digi-Key\dpn_296-6501-6-ND_US_en_USD_US.dat
2024-01-25 17:19:01,445 - kicost_digikey_api_v -    DEBUG: Using API V3
2024-01-25 17:19:01,445 - kicost_digikey_api_v -  WARNING: Oauth2 token storage does not exist or malformed, creating new.
2024-01-25 17:19:01,445 - kicost_digikey_api_v -    DEBUG: AUTH - Authenticating with endpoint https://sandbox-api.digikey.com/v1/oauth2/authorize using ID: XXXXXXXX...
2024-01-25 17:19:01,445 - kicost_digikey_api_v -    DEBUG: AUTH - Redirect URL: https://localhost:8139/digikey_callback
2024-01-25 17:19:10,599 - kicost_digikey_api_v -    DEBUG: TOKEN - Exchanging F0XXQeIo auth code for token at endpoint: https://sandbox-api.digikey.com/v1/oauth2/token
2024-01-25 17:19:10,599 - kicost_digikey_api_v -    DEBUG: TOKEN - Using client id: XXXXXXXX...
2024-01-25 17:19:10,599 - kicost_digikey_api_v -    DEBUG: TOKEN - Using client secret: XXXXXXXX...
Traceback (most recent call last):
  File "C:\Users\username\git\kicost-digikey-api-v3\kicost_digikey_api_v3\oauth\oauth2.py", line 194, in __exchange_for_token
    r.raise_for_status()
  File "C:\Users\username\.conda\envs\fresh-env\lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://sandbox-api.digikey.com/v1/oauth2/token

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\username\git\kicost-digikey-api-v3\test_production.py", line 29, in <module>
    part = o.search()
  File "C:\Users\username\git\kicost-digikey-api-v3\kicost_digikey_api_v3\utils.py", line 147, in search
    result = kicost_digikey_api_v3.product_details(self.dk_pn, api_limits=self.api_limit, includes=includes, **DK_API.extra_ops)
  File "C:\Users\username\git\kicost-digikey-api-v3\kicost_digikey_api_v3\v3\api.py", line 107, in product_details
    client = DigikeyApiWrapper('product_details_with_http_info', kicost_digikey_api_v3.v3.productinformation)
  File "C:\Users\username\git\kicost-digikey-api-v3\kicost_digikey_api_v3\v3\api.py", line 50, in __init__
    self._digikeyApiToken = kicost_digikey_api_v3.oauth.oauth2.TokenHandler(version=3, sandbox=self.sandbox).get_access_token()
  File "C:\Users\username\git\kicost-digikey-api-v3\kicost_digikey_api_v3\oauth\oauth2.py", line 301, in get_access_token
    token_json = self.__exchange_for_token(httpd.auth_code)
  File "C:\Users\username\git\kicost-digikey-api-v3\kicost_digikey_api_v3\oauth\oauth2.py", line 196, in __exchange_for_token
    raise DigikeyOauthException('TOKEN - Cannot request new token with auth code: {}'.format(e))
kicost_digikey_api_v3.exceptions.DigikeyOauthException: TOKEN - Cannot request new token with auth code: 403 Client Error: Forbidden for url: https://sandbox-api.digikey.com/v1/oauth2/token

PS.:
Before clicking in the "Allow" buttom this two certificate files are saved in the ~./.cache/kicost/Digi-Key folder.
image

And after the error occurs the certificate files in the ~./.cache/kicost/Digi-Key folder are gone.

The file config.txt:

DIGIKEY_CLIENT_ID=XXXXXXXXXXXXXXXXc84M
DIGIKEY_CLIENT_SECRET=XXXXXXKs3f
DIGIKEY_CLIENT_SANDBOX=True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions