How to extend facebook access token in python

不羁岁月 提交于 2019-12-03 03:41:39

Not sure if this was available in python's FB API when the question was originally asked, but a neater approach to extend the expiry of the access token would be:

graph = facebook.GraphAPI(user_short_lived_token_from_client)
app_id = 'app_id' # Obtained from https://developers.facebook.com/
app_secret = 'app_secret' # Obtained from https://developers.facebook.com/

# Extend the expiration time of a valid OAuth access token.
extended_token = graph.extend_access_token(app_id, app_secret)
print extended_token #verify that it expires in 60 days
kyrenia

here's an edited version , compatible with latest api versions:

import requests
import json
access_token = 'your token'     # Obtained from https://developers.facebook.com/tools/accesstoken/
app_id = "your app id"          # Obtained from https://developers.facebook.com/        
client_secret = "app secret"    # Obtained from https://developers.facebook.com/

link = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=" + app_id +"&client_secret=" + client_secret + "&fb_exchange_token=" + access_token
s = requests.Session()
token = s.get(link).content
token=json.loads(token)
token=token.get('access_token')

print token

According to their subsection on extending short lived client tokens, you'll need to take in your short lived client token and, having filled in the relevant app data, send a GET request from your server to the following endpoint:

GET /oauth/access_token?  
  grant_type=fb_exchange_token&           
  client_id={app-id}&
  client_secret={app-secret}&
  fb_exchange_token={short-lived-token}

The response will contain your long-lived access token which can then be passed back to the client or used on your server. If you don't currently have a module for performing HTTP operations, I highly recommend Requests.

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