简体   繁体   中英

urllib.request- HTTPError: HTTP Error 403: Forbidden

I'm trying to automate downloads of specific part prices and quantities from Octopart using Python. I'm able to convert the csv file with the specific part numbers I want to look up into line items and queries, but keep getting an HTTPError message when I try to send the queries to REST API for part matching. I entered in my apikey but since it still doesn't connect, I'm wondering if I wrote the url incorrectly. Any guidance would be appreciated.

Code:

 # Send queries to REST API for part matching.
import json
import urllib.parse
import urllib.request

results = []
for i in range(0, len(queries), 20):
# Batch queries in groups of 20, query limit of
# parts match endpoint
batched_queries = queries[i: i + 20]

url = 'http://octopart.com/api/v3/parts/match?queries=%s' \
    % urllib.parse.quote(json.dumps(batched_queries))
url += '&apikey=eb49732b'
data = urllib.request.urlopen(url)
response = json.loads(data)

# Record results for analysis
results.extend(response['results'])

Error:

    HTTPError                                 Traceback (most recent call last)
<ipython-input-43-cf5776fdc754> in <module>()
     14     url = 'http://octopart.com/api/v3/parts/match?queries=%s'         % urllib.parse.quote(json.dumps(batched_queries))
     15     url += '&apikey=eb49732b'
---> 16     data = urllib.request.urlopen(url)
     17     response = json.loads(data)
     18 

~\Documents\Software\Anaconda\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

~\Documents\Software\Anaconda\lib\urllib\request.py in open(self, fullurl, data, timeout)
    529         for processor in self.process_response.get(protocol, []):
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 
    533         return response

~\Documents\Software\Anaconda\lib\urllib\request.py in http_response(self, request, response)
    639         if not (200 <= code < 300):
    640             response = self.parent.error(
--> 641                 'http', request, response, code, msg, hdrs)
    642 
    643         return response

~\Documents\Software\Anaconda\lib\urllib\request.py in error(self, proto, *args)
    567         if http_err:
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    570 
    571 # XXX probably also want an abstract factory that knows when it makes

~\Documents\Software\Anaconda\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
    501         for handler in handlers:
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:
    505                 return result

~\Documents\Software\Anaconda\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs)
    647 class HTTPDefaultErrorHandler(BaseHandler):
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    650 
    651 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 403: Forbidden

Thank you for your help!

check your API key or contact them and ask about permissions

When I tried with curl sample using your key, it also fails with 403

$  curl -G https://octopart.com/api/v3/parts/match -d queries="[{\"mpn\":\"SN74S74N\"}]" \
-d apikey=eb49732b \
-d pretty_print=true
{
  "__class__": "ClientErrorResponse",
  "message": "Forbidden request"
}

However with EXAMPLE_KEY the query above succeeds

Try the following code with your api key... if it doesn't work then your key is probably invalidated.

import json
import urllib
import urllib.parse
import urllib.request

queries = [
    {'mpn': 'SN74S74N',
     'reference': 'line1'},
    {'sku': '67K1122',
     'reference': 'line2'},
    {'mpn_or_sku': 'SN74S74N',
     'reference': 'line3'},
    {'brand': 'Texas Instruments',
     'mpn': 'SN74S74N',
     'reference': 'line4'}
    ]

url = 'http://octopart.com/api/v3/parts/match?queries=%s' \
    % urllib.parse.quote(json.dumps(queries))
url += "&include[]=specs"

# NOTE: Use your API key here (https://octopart.com/api/register)
url += '&apikey=<REPLACEME>'

data = urllib.request.urlopen(url).read()
response = json.loads(data)

# print request time (in milliseconds)
print("Response time: %s msec\n" % response['msec'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM