如何在python中扩展我的Facebook图API API? [英] How to extend my facebook graph API token in python?

查看:106
本文介绍了如何在python中扩展我的Facebook图API API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python挖掘Facebook。直到现在,我通过这个网站获取了图形API 1.0版的访问令牌(我需要它为1.0):



https://developers.facebook.com/tools/explorer/?method=GET& ; path = me& version = v1.0



但是,在这个网站上得到的令牌会在1个小时内到期,所以我该如何扩展这个令牌在python?我已经看到了如何在php,但不幸的是,我必须坚持只有python为我的项目

解决方案

Facebook有4令牌类型:用户,应用,页面和客户端( https://开发人员.facebook.com /文档/ Facebook的登录/接入令牌#usertokens 的)。这些令牌类型中的每一个都有自己的权限。例如,您可以代表用户(一旦他们授予您权限)使用应用访问令牌发布,但需要用户访问令牌来拉取用户/状态。 Web用户访问令牌是短暂的(约1-2小时),需要转换为长时间(60天)访问令牌。



在python假设您正在使用facebook-sdk(pip install facebook-sdk),您可以扩展令牌服务器端调用GraphAPI extend_access_token方法。



views.py: p>

  import facebook 

user_access_token ='USER_ACCESS_TOKEN'#最初生成的客户端
app_id =' YOUR_UNIQUE_APP_ID'#在developer.facebook.com找到
app_secret ='YOUR_APP_SECRET'#在developer.facebook.com找到

#创建fb图形对象
graph = facebook.GraphAPI( user_access_token)

#现在使用extend_access_token方法扩展它
extended_token = graph.extend_access_token(app_id = app_id,app_secret = app_secret)

#确认令牌现在到期〜60天或5184000秒
打印extended_token

现在令牌扩展ed,您可以代表用户存储和使用活动的Web客户端。


I´m currently mining facebook using python. Until now, I´m getting an access token for graph API version 1.0(I need it to be 1.0) via this website:

https://developers.facebook.com/tools/explorer/?method=GET&path=me&version=v1.0

but the token I get on this site expires in about 1 hour, so how can I extend this token in python? I already saw how to in php, but, unfotunately, I have to stick with only python for my project

解决方案

Facebook has 4 types of tokens: user, app, page and client (https://developers.facebook.com/docs/facebook-login/access-tokens#usertokens). Each of these token types have their own permissions. For example, you can post on behalf of a user (once they've granted you permission) using the app access token but need a user access token to pull user/statuses. The web user access token are short-lived (~1-2 hrs) and need to be converted to a long-lived (60 days) access token.

In python you can extend the token server-side calling the GraphAPI extend_access_token method assuming you are using the facebook-sdk (pip install facebook-sdk).

views.py:

import facebook

user_access_token = 'USER_ACCESS_TOKEN' # initially generated client-side
app_id = 'YOUR_UNIQUE_APP_ID' # found at developer.facebook.com
app_secret = 'YOUR_APP_SECRET' # found at developer.facebook.com

# Create fb graph object
graph = facebook.GraphAPI(user_access_token)

# Now extend it with the extend_access_token method
extended_token = graph.extend_access_token(app_id=app_id, app_secret=app_secret)

# Confirm token now expires in ~ 60 days or 5184000 seconds
print extended_token

Now that the token is extended, you can store and use on the users behalf w/out an active web client.

这篇关于如何在python中扩展我的Facebook图API API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆