我是 Facebook 应用程序开发人员,但我是一位经验丰富的开发人员。我使用 web.py 作为我的 web 框架,更糟糕的是,我是 Python 新手。
我遇到了一个问题,当我尝试切换到使用较新的“OAuth 2.0 for Canvas”时,我根本无法工作。在我的 Facebook 应用程序中返回的唯一内容是“无”。
我启用 OAuth 2.0 的动机是因为听起来 Facebook 会在 7 月之前强制它,我不妨现在学习它,现在必须在几周内重写它。
我在高级设置中打开了“OAuth 2.0 for Canvas”,并重写了我的代码以查找每当我的测试用户尝试访问我的应用程序时发布到我的服务器的“signed_request”。
我的代码如下(为简洁起见,我删除了调试语句和错误检查):
#!/usr/bin/env python
import base64
import web
import minifb
import urllib
import json
FbApiKey = "AAAAAA"
FbActualSecret = "BBBBBB"
CanvasURL = "http://1.2.3.4/fb/"
RedirectURL="http://apps.facebook.com/CCCCCCCC/"
RegURL = 'https://graph.facebook.com/oauth/authorize?client_id=%s&redirect_uri=%s&type=user_agent&display=page' % (FbApiKey, RedirectURL)
urls = (
'/fb/', 'index',
)
app = web.application(urls, locals())
def authorize():
args = web.input()
signed_request = args['signed_request']
#split the signed_request via the .
strings = signed_request.split('.')
hmac = strings[0]
encoded = strings[1]
#since uslsafe_b64decode requires padding, add the proper padding
numPads = len(encoded) % 4
encoded = encoded + "=" * numPads
unencoded = base64.urlsafe_b64decode(str(encoded))
#convert signedRequest into a dictionary
signedRequest = json.loads(unencoded)
try:
#try to find the oauth_token, if it's not there, then
#redirect to the login page
access_token = signedRequest['oauth_token']
print(access_token)
except:
print("Access token not found, redirect user to login")
redirect = "<script type=\"text/javascript\">\ntop.location.href=\"" +_RegURL + "\";\n</script>"
print(redirect)
return redirect
# Do something on the canvas page
returnString = "<html><body>Hello</body></html>"
print(returnString)
class index:
def GET(self):
authorize()
def POST(self):
authorize()
if __name__ == "__main__":
app.run()
暂时我想专注于用户已经登录的情况,所以假设找到了oauth_token。
我的问题是:为什么我的“Hello”没有被输出,而我看到的只是“None”?
看来我错过了一些非常基本的东西,因为我向你发誓,我已经在互联网上搜索了解决方案,并且我已经多次阅读了 Facebook 页面。同样,我发现了许多很好的博客和 stackoverflow 问题,它们准确地记录了如何使用 OAuth 2.0 和 signed_request。但事实上我得到了一个正确的 oauth_token,但我唯一的输出是“无”,这让我认为我做错了一些基本的事情。我意识到“无”是python中的一个特殊词,所以也许这就是原因,但我无法确定我做错了什么。
当我关闭 OAuth 2.0 并恢复我的代码以查找较旧的 POST 数据时,我可以轻松地将内容打印到屏幕上。
对此的任何帮助将不胜感激!