pythonfacebook-graph-apiapp-secret

facebook graph api calls with appsecret_proof in python


What is the right way of making graph api calls with appsecret_proof parameter in python? Is there any library that allows such thing?

I was trying to use 'python for facebook' library but the documentation is literally nonexistent so I can't figure it out.


Solution

  • Here's how you could do that using the facebook-sdk:

    import facebook
    import hashlib
    import hmac
    
    def genAppSecretProof(app_secret, access_token):
        h = hmac.new (
            app_secret.encode('utf-8'),
            msg=access_token.encode('utf-8'),
            digestmod=hashlib.sha256
        )
        return h.hexdigest()
    
    app_secret = "xxxxxxxxx"
    access_token = "xxxxxxxxx"
    api = facebook.GraphAPI(access_token)
    msg = "Hello, world!"
    postargs = {"appsecret_proof": genAppSecretProof(app_secret, access_token)}
    status = api.put_wall_post(msg, postargs)
    

    Tested with Python 2.7.9 and facebook-sdk 1.0.0-alpha.