peeter123 / digikey-api

Python module for the Digikey PartSearch API
GNU General Public License v3.0
84 stars 35 forks source link

Issues with example code #49

Closed benjamano closed 5 months ago

benjamano commented 5 months ago

Hi Everyone,

I've been trying to use this API, but keep running into this error when running the code below:

I've tried the V3 (ProductInformation) and the V4 (Product Information) to troubleshoot, both have the same error.

The authorization browser window opens but the code crashes straight after

Code:

import os
from pathlib import Path

import digikey
from digikey.v3.productinformation import KeywordSearchRequest
from digikey.v3.batchproductdetails import BatchProductDetailsRequest

CACHE_DIR = Path('path/to/cache/dir')

os.environ['DIGIKEY_CLIENT_ID'] = 'EBfFnLvf0eUfSR9PGTSB5kngims1Ulsp'
os.environ['DIGIKEY_CLIENT_SECRET'] = '0ogUpOFq9Gs0gAk2'
os.environ['DIGIKEY_CLIENT_SANDBOX'] = 'True'
os.environ['DIGIKEY_STORAGE_PATH'] = r'C:\Users\benme\OneDrive\Careers or Jobs\3ADesigns'

# Query product number
dkpn = '296-6501-1-ND'
part = digikey.product_details(dkpn)

# Search for parts
search_request = KeywordSearchRequest(keywords='CRCW080510K0FKEA', record_count=10)
result = digikey.keyword_search(body=search_request)

# Search for Batch of Parts/Product
mpn_list = ["0ZCK0050FF2E", "LR1F1K0"] #Length up to 50
batch_request = BatchProductDetailsRequest(products=mpn_list)
part_results = digikey.batch_product_details(body=batch_request)

Error:

`Oauth2 token storage does not exist or malformed, creating new.
Traceback (most recent call last):
  File "c:\Users\benme\OneDrive\Careers or Jobs\3ADesigns\DigikeyV3.py", line 18, in <module>
    part = digikey.product_details(dkpn)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\benme\AppData\Local\Programs\Python\Python312\Lib\site-packages\digikey\v3\api.py", line 121, in product_details
    client = DigikeyApiWrapper('product_details_with_http_info', digikey.v3.productinformation)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\benme\AppData\Local\Programs\Python\Python312\Lib\site-packages\digikey\v3\api.py", line 55, in __init__
    self._digikeyApiToken = digikey.oauth.oauth2.TokenHandler(version=3, sandbox=self.sandbox).get_access_token()
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\benme\AppData\Local\Programs\Python\Python312\Lib\site-packages\digikey\oauth\oauth2.py", line 255, in get_access_token
    httpd.socket = ssl.wrap_socket(httpd.socket, certfile=str(Path(filename)), server_side=True)
                   ^^^^^^^^^^^^^^^
AttributeError: module 'ssl' has no attribute 'wrap_socket'

Any help would be appreciated,

Thanks very much,

Ben

benjamano commented 5 months ago

Fixed, I think my SSL library installed with python was corrupt, I set up a virtual environment inside WSL and copied the SSL library code from there and pasted it into my main window system's SSL code.