I am trying to get a response from an internal url which I can access through my laptop using a web-browser.
s = requests.Session()
r = s.get(url_1, auth=auth, verify=False)
print r.text
the reply i get is: 401 - unauthorized.
It's obviously going to be difficult to debug an HTTP 401 Unauthorized as we don't have access to the internal URL. Your code looks correct to me so I'm assuming this is a real 401 Unauthorized which means the request has incorrect authentication credentials. My advice would be to make sure you have reviewed the Python Requests docs on authentication and consider that your request is likely going through a proxy so the Requests docs on proxy config might be helpful.
Related
I am getting 403 error while trying run GET function on Rest Api to pull data in python
import requests
url = 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=803101&date=13-05-2021'
ploads = {'pincode':'803101', 'date':'13-05-2021'}
r = requests.get(url, params = ploads)
data = r.text
403 means forbidden means the server understands your request but forbid your access. Please check if you have required access to the resource you are trying to access.
Don't confuse this with 401 which happen when you don't have authorization.
When send the post request it returns a 401 error. Is there anything in the setup that might be causing the error. Here is the code:
import requests
>>>url= 'https://us-street.api.smartystreets.com/street-address'
>>>payload = {'auth-id':'xxxxxx','auth-token':'xxxxx'}
>>>body = [{"street":"1 Santa Claus","city":"North Pole","state":"AK","candidates":10},{"addressee":"Apple Inc","street":"1 infinite loop","city":"cupertino","state":"CA","zipcode":"95014","candidates":10}]
>>>headers = {'Content-Type':'application/json; charset=utf-8', 'Host':'us-street.api.smartystreets.com'}
>>>r = request.post(url, data=payload, json=body, headers=headers)
>>>r.status_code
401
>>>r.url
https://us-street.api.smartystreets.com/street-address
Any help would be appreciated.
Link to api documentation: https://smartystreets.com/docs/cloud/us-street-api
According to the documentation, your auth credentials should be in the URL's query string, not in the body. So use params=payload instead of data=payload.
Basically, as the API does not find your auth credentials in the right place, it assumes you are not authenticaled/signed in, hence the 401 status code.
I am trying to authenticate using API key and Secret but it is showing 403 error. Below is the code
headers = {'X-API-KEY': 'someapikey', 'X-API-SECRET': 'somesecretkey'}
url="http://ops.epo.org/rest-services/publisheddata/publication/epodoc/EP1000000/fulltext"
r = requests.get(url, headers=headers)
The error message is :
b'<error><code>403</code><message>This request has been rejected due to the
violation of Fair Use policy</message><moreInfo>http://www.epo.org/searching
/free/espacenet/fair-use.html</moreInfo>\n\t\t\t\t</error>\n\t\t\t'
Please help!!
There is no problem with your headers, you've sent too many requests and got a temporary ban. You have to wait for some time and try again. The correct link to fair use policy is https://www.epo.org/service-support/ordering/fair-use.html
Read the "Automated queries" section and consider adding some throttling to your script.
I am attempting to get user statistics from the Fortnite tracker api.
I have an api key and am using the correct url as indicated in the documentation
Template url:
https://api.fortnitetracker.com/v1/profile/{platform}/{epic-nickname}
Desired url:
https://api.fortnitetracker.com/v1/profile/pc/xantium0
If I use this link in browser I get {"message":"No API key found in request"} (as I have not passed the API key) so the link should be correct. Also if I do not pass the api key with urllib then I still get a 403 error.
I have checked out how to pass a header in a request: How do I set headers using python's urllib?
and so far have this code:
import urllib.request as ur
request = ur.Request('https://api.fortnitetracker.com/v1/profile/pc/xantium0', headers={'TRN-Api-Key' : 'xxx'})
response = ur.urlopen(request)
print(response.read())
When run I get this error:
urllib.error.HTTPError: HTTP Error 403: Forbidden
403 checks out as:
HTTP 403 is a standard HTTP status code communicated to clients by an HTTP server to indicate that the server understood the request, but will not fulfill it. There are a number of sub-status error codes that provide a more specific reason for responding with the 403 status code.
https://en.wikipedia.org/wiki/HTTP_403
The response is the same if I don't pass the api key in the header.
I can only think of three reasons this code is not working:
I have passed the wrong header name (i.e. it's not TRN-Api-Key)
My code is incorrect and I am not actually passing a header to the server
I have been banned
My problem is that I think my code is correct:
From the documentation:
urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)
I have passed the url and I have passed the headers (wihout confusing with the data arguement). The api documentation also mentions it should be passed in the headers.
I am also quite sure I need to use the TRN-Api-Key as it is shown in the api documentation:
TRN-Api-Key: xxx
Also in this question (using Ruby):
header = {
key: "TRN-Api-Key: Somelong-api-key-here"
}
Or I have been banned (this is possible although I got the key 15 minutes ago) is there a way to check? Would this error be returned?
What is preventing me from getting the user statistics?
Try using requests, a pythonic, fast and widely used module.
import requests
url = 'https://api.fortnitetracker.com/v1/profile/pc/xantium0'
headers = {
'TRN-Api-Key' : 'xxx'
}
response = requests(url, headers=headers)
print('Requests was successful:', response.ok)
print(response.text)
If it doesn't work you can visit the url with your browser, then check the requests:
in Firefox press Cntrl+Shift+E, in Chrome Cntrl+E (or Inspect with Cntrl+Shift+I and then go to Network). Press on "https://api.fortnitetracker.com/v1/profile/pc/xantium0" and change the headers. On Firefox there's the button Modify and resend. Check the response and eventually, try to change the header api key name.
Hope this helps, let me know.
I am using python requests library for a POST request and I expect a return message with an empty payload. I am interested in the headers of the returned message, specifically the 'Location' attribute. I tried the following code:
response=requests.request(method='POST', url=url, headers={'Content-Type':'application/json'}, data=data)
print response.headers ##Displays a case-insensitve map
print response.headers['Location'] ##blows up
Strangely the 'Location' attribute is missing in the headers map. If I try the same POST request on postman, I do get a valid Location attribute. Has anyone else seen this? Is this a bug in the requests library?
Sounds like everything's working as expected? Check your response.history
From the Requests documentation:
Requests will automatically perform location redirection for all verbs except HEAD.
>>> r = requests.get('http://github.com')
>>> r.url
'https://github.com/'
>>> r.status_code
200
>>> r.history
[<Response [301]>]
From the HTTP Location page on wikipedia:
The HTTP Location header field is returned in responses from an HTTP server under two circumstances:
To ask a web browser to load a different web page. In this circumstance, the Location header should be sent with an HTTP status code of 3xx. It is passed as part of the response by a web server when the requested URI has:
Moved temporarily, or
Moved permanently
To provide information about the location of a newly-created resource. In this circumstance, the Location header should be sent with an HTTP status code of 201 or 202.1
The requests library follows redirections automatically.
To take a look at the redirections, look at the history of the requests. More details in the docs.
Or you pass the extra allow_redirects=False parameter when making the request.