Publishing on Facebook fan page with Python
Question: I tried a couple of codes about how to publish on Facebook wall. But I would
like do a little bit different. I wonder publish in my facebook fan page. The
following code just publish on my personal profile. Can any one give me a clue
to publish in fan page?

    
    
    #!/usr/bin/python
    
    import facebook
    import urllib 
    import urlparse
    
    FACEBOOK_APP_ID = 'X'
    FACEBOOK_APP_SECRET = 'Y'
    FACEBOOK_PROFILE_ID = 'MyProfileId (**not page id, right?**)'
    
    oauth_args = dict(client_id     = FACEBOOK_APP_ID,
                      client_secret = FACEBOOK_APP_SECRET,
                      grant_type    = 'client_credentials')
    
    oauth_response = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)).read()                                  
    page_token='PAGE TOKEN GOT INhttps://graph.facebook.com/SITE' 
    fields=access_token
    attach = {
      "name": 'Hello world',
      "link": 'http://www.example.com',
      "caption": 'test post',
      "description": 'some test',
      "picture" : 'http://www.example.com/picture.jpg',
      "page_token" : page_token
    }
    try:
        oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0]
    except KeyError:
        raise
    print oauth_access_token
    facebook_graph = facebook.GraphAPI(oauth_access_token)
    try:
        response = facebook_graph.put_wall_post('', attachment=attach,profile_id = FACEBOOK_PROFILE_ID)
    except facebook.GraphAPIError as e:
        print e
    


Answer: I corrected the code, here is the solution:

    
    
    #!/usr/bin/python
    # coding: utf-8
    
    import facebook
    import urllib 
    import urlparse
    
    access_token_page='X'
    FACEBOOK_APP_ID = 'Y'
    FACEBOOK_APP_SECRET = 'Z'
    FACEBOOK_PROFILE_ID = 'W'
    
    oauth_args = dict(client_id     = FACEBOOK_APP_ID,
                      client_secret = FACEBOOK_APP_SECRET,
                      grant_type    = 'client_credentials')
    
    oauth_response = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)).read()                                  
    
    attach = {
      "name": 'Hello world',
      "link": 'http://www.example.com',
      "caption": 'test post',
      "description": 'some test',
      "picture" : 'http://www.example.com/picture.jpg',
    }
    
    
    facebook_graph = facebook.GraphAPI(access_token_page)
    try:
        response = facebook_graph.put_wall_post('', attachment=attach)
    except facebook.GraphAPIError as e:
        print e
    

Information about authentication can be get in:
<https://developers.facebook.com/docs/authentication/pages/>

