0

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?

1''
  • 26,823
  • 32
  • 143
  • 200
  • 1
    And what's the text of the error response? knowing the HTTP error code isn't enough – Igy Aug 02 '13 at 00:17
  • @Igy Thanks for the suggestion - I wasn't aware that I could get more than just the error code, since I'm new to web development. – 1'' Aug 02 '13 at 00:52

2 Answers2

2

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.

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200
1

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.

gr3co
  • 893
  • 1
  • 7
  • 15