I have successfully used the following Python code, running on a Google App Engine server on localhost, to send Facebook notifications to myself. I use the template feature of Facebook notifications to expand a Facebook user ID into a bolded name in the text of the notification:
url = 'https://graph.facebook.com/'+myOwnID+'/notifications'
values = {'access_token' : 426547656256546|4fhe34FJdeV3WvfF6SNfehs7GfW
'href' : 'http://localhost:8080/',
'template' : '#['+myOwnID+'] says hi.'}
req = urllib2.Request(url, urllib.urlencode(values))
urllib2.urlopen(req)
Note that the app access token is made-up, but has the same format as a real token.
When I change the template's ID to the ID of one of my friends:
url = 'https://graph.facebook.com/'+myOwnID+'/notifications'
values = {'access_token' : 426547656256546|4fhe34FJdeV3WvfF6SNfehs7GfW
'href' : 'http://localhost:8080/',
'template' : '#['+myFriendID+'] says hi.'}
req = urllib2.Request(url, urllib.urlencode(values))
urllib2.urlopen(req)
I get the error
HTTP Error 403: Forbidden
It works the same even if I hard-code the IDs into the template, so it's not an issue of incorrect variable values.
Why does the second case not work? How can I fix it?
I didn't realize that urllib2 can print a more detailed error message than just the status code, by replacing the last line with:
try:
urllib2.urlopen(req)
except urllib2.HTTPError, error:
logging.info(error.read()) # or "print error.read()"
This gives the clear error message:
{"error":{"message":"(#200) Cannot tag users who have not installed the app","type":"OAuthException","code":200}}
For a fuller discussion of urllib2's behaviour with 403 status codes, see 1, 2 and 3.
It appears, according to your error message, that your friend has to install your app in order for you to send them notifications. There's nothing you can do to fix that.
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.
I'm trying to access AWX API from a script Python.
The documentation has the ressource /api/v1/authtoken/ for that, however when visiting the URL:
https://myHost/api/v1/authtoken/
It says that it can't find the ressource.
I also tried:
response = requests.get('https://myHost/api/login/', verify=False,
data = json.dumps({"username": "user","password": "pass"}))
results = json.loads(response.text)
token = results['token']
But I get a :
ValueError: No JSON object could be decoded
AWX version: 10.0.0
The fine manual says that:
A GET to /api/login/ displays the login page of API browser
So = requests.get( is for sure not what you want; however, even if you were to switch to requests.post the very next line says:
It should be noted that the POST body of /api/login/ is not in JSON, but in HTTP form format. Four items should be provided in the form:
so data = json.dumps({ is also for sure also not what you want
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 Requests API with Python2.7.
I am trying to download certain webpages through proxy servers. I have a list of available proxy servers. But not all proxy servers work as desired. Some proxies require authentication, others redirect to advertisement pages etc. In order to detect/verify incorrect responses, I have included two checks in my url requests code. It looks similar to this
import requests
proxy = '37.228.111.137:80'
url = 'http://www.google.ca/'
response = requests.get(url, proxies = {'http' : 'http://%s' % proxy})
if response.url != url or response.status_code != 200:
print 'incorrect response'
else:
print 'response correct'
print response.text
There are some proxy servers with which the requests.get call is successful and they pass these two conditions and still contain invalid html source in response.text attribute. However, if I use the same proxy in my FireFox browser and try to open the same webpage, I am displayed an invalid webpage, but my python script says that the response should be valid.
Can someone point to me that what other necessary checks I am missing to weed out incorrect html results?
or
How can I successfully verify if the webpage I intended to receive is correct?
Regards.
What is an "invalid webpage" when displayed by your browser? The server can return a HTTP status code of 200, but the content is an error message. You understand it to be an error message because you can comprehend it, a browser or code can not.
If you have any knowledge about the content of the target page, you could check whether the returned HTML contains that content and accept it on that basis.