skip to Main Content

The following python code:

  # user profile information
  args = {
    'access_token':access_token,
    'fields':'id,name',
  }
  print 'ACCESSED', urllib.urlopen('https://graph.facebook.com/me', urllib.urlencode(args)).read()

Prints the following:

ACCESSED {“success”:true}

The token is valid, no error, the fields are valid. Why is it not returning the fields I asked for?

2

Answers


  1. Chosen as BEST ANSWER

    Turns out urllib.urlopen will send the data as a POST when the data parameter is provided. Facebook Graph API works using GET not POST. Change the call to trick the function into calling just a URL ( no data ):

    print 'ACCESSED', urllib.urlopen('https://graph.facebook.com/me/?' + urllib.urlencode(args)).read()
    

    And everything works! Sigh, I can see why urllib is being altered in python 3.0...


  2. You have to add a / to the URL to get https://graph.facebook.com/me/ instead of https://graph.facebook.com/me.

    # user profile information
    args = {
        'access_token':access_token,
        'fields':'id,name'
    }
    
    print 'ACCESSED', urllib.urlopen('https://graph.facebook.com/me/', urllib.urlencode(args)).read()
    

    PS : Try using the requests which is much more efficient than urllib, here is the doc : http://docs.python-requests.org/en/master/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search