我正在构建一个用于对网站(如 FB、LinkedIn 等)进行 API 调用的应用程序。我需要使用 OAuth 来授权我的应用程序代表用户请求数据。我遇到了跨 View 存储我的网站界面库 (LinkedIn) 实例的问题。我使用了 request.session - 带有文件后端。
下面是代码http://pastebin.com/QTgqSr7W
我做错了什么吗?可以看到在 login() 中设置的值,但我在 token() 中看不到相同的值。这是错误的期望吗?传递 api 实例值的任何解决方法?
感谢和问候, 阿图尔。
请您参考如下方法:
嗯,我认为这是因为你正在保存整个 api python 实例,我认为 session 不支持那种数据,为什么不直接将用户重定向到 auth url 而无需在 session 中保存某些内容,然后在回调 View 中实例化linkedin.LinkedIn 类
from django.conf import settings
key = settings.KEY
secret = settings.SECRET
return_url = settings.CALLBACK
# You make the api connection here, so its not tied to any function
api = linkedin.LinkedIn(key, secret, return_url)
def login(request):
if api.request_token():
auth_url = api.get_authorize_url()
return HttpResponseRedirect(auth_url)
#below is the view that will get called with the oauth oken.
def token(request, param):
#do stuff with the api.
