facebook graph api calls with appsecret_proof in python

两盒软妹~` 提交于 2021-02-07 04:20:08

问题


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.


回答1:


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.




回答2:


Doing this without the facebook SDK quite simple... the key is that your facebook_app_token is a concatenation if your app_id and app_secret combined with a |.

import hmac,hashlib
facebook_app_id     = '<YOUR_APP_ID>'
facebook_app_secret = '<YOUR_APP_SECRET>'
facebook_app_token  = '{}|{}'.format(facebook_app_id,facebook_app_secret)
app_secret_proof    = hmac.new(facebook_app_secret.encode('utf-8'),
                           msg=facebook_app_token.encode('utf-8'),
                           digestmod=hashlib.sha256).hexdigest()

Then you can include the app_secret_proof in whatever API you're calling... e.g.,

import requests
base_url = 'https://graph.facebook.com/v2.10/<USER_ID>/ids_for_pages?access_token={}&appsecret_proof={}'
url = base_url.format(facebook_app_token,app_secret_proof)
result = requests.get(url)
result.json()



回答3:


This question is too old, but I found other way to generate app_secret_proof using this python SDK.

    from facebook_business import session
    fb_session = session.FacebookSession(app_id, app_secret, access_token)
    app_secret_proof = fb_session.appsecret_proof


来源:https://stackoverflow.com/questions/26248105/facebook-graph-api-calls-with-appsecret-proof-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!