Programmatically getting an access token for using the Facebook Graph API

前端 未结 8 1001
我在风中等你
我在风中等你 2020-12-04 09:06

I am trying to put together a bash or python script to play with the facebook graph API. Using the API looks simple, but I\'m having trouble setting up curl in my bash scrip

相关标签:
8条回答
  • 2020-12-04 09:46

    Easy! Just use facebook-sdk.

    import facebook
    
    app_id = 'YOUR_APP_ID'
    app_secret = 'YOUR_APP_SECRET'
    
    graph = facebook.GraphAPI()
    
    # exactly what you're after ;-)
    access_token = graph.get_app_access_token(app_id, app_secret) 
    
    0 讨论(0)
  • 2020-12-04 09:48

    Here you go, as simple as it can get. Doesn’t require any 3rd-party SDK etc.

    Make sure Python 'requests' module is installed

    import requests
    
    def get_fb_token(app_id, app_secret):
        url = 'https://graph.facebook.com/oauth/access_token'       
        payload = {
            'grant_type': 'client_credentials',
            'client_id': app_id,
            'client_secret': app_secret
        }
        response = requests.post(url, params=payload)
        return response.json()['access_token']
    
    0 讨论(0)
提交回复
热议问题