2012-02-29 86 views
0

我正在尝试实施Facebook实时API。 我已经成功添加了订阅。 (请访问网址以了解如何... What is verify token in Facebook Realtime API成功订阅后处理来自Facebook的通知请求

但我不会从FB得到任何通知。 每当用户进行任何更改时,FB都会对我的callback_url进行POST调用(我检查了我的站点的访问日志)。但我无法将数据写入文件。

我的代码是:

from datetime import datetime 
def fb_notifications(request): 
    handle1=open('/path_to_log_file/smt_logs.log','a+') 
    handle1.write('\n<<<<<Log accessed at ' + str(datetime.now()) + '>>>>>>') 
    handle1.close(); 
    if request.GET: (#This is working properly.....) 
     handle1=open('/var/smt_logs/smt_logs.txt','a+') 
     handle1.write('\n---------------Subscription STARTS at ' + str(datetime.now()) + ' ----------------------------' + '\n') 
     handle1.write(str(request)) 
     handle1.write('\n-----------------END--------------------------' + '\n\n') 
     handle1.close(); 
     code = request.GET.get('hub.challenge')  
     return HttpResponse(code) 
    elif request.POST:(#This is not working... Trying to write data to a File....) 
     handle1=open('/path_to_log_file/smt_logs.log','a+') 
     handle1.write('\n---Notification STARTS at ' + str(datetime.now()) + ' ---' + '\n') 
     handle1.write(str(request)) 
     handle1.write('\n---END---' + '\n\n') 
     handle1.close(); 
    else: 
     handle1=open('/path_to_log_file/smt_logs.log','a+') 
     handle1.write('\n---Error at ' + str(datetime.now()) + ' ---' + '\n') 
     handle1.write('\n---END---' + '\n\n') 
     handle1.close(); 
     return HttpResponse('Error! Please try again.') 

日志文件:

66.220.145.247 - - [29/Feb/2012:13:05:03 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.155.117 - - [29/Feb/2012:13:05:47 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2509 "-" "-" 
66.220.145.247 - - [29/Feb/2012:13:06:38 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.145.250 - - [29/Feb/2012:13:08:42 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.151.123 - - [29/Feb/2012:13:08:45 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.151.121 - - [29/Feb/2012:13:09:14 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.145.245 - - [29/Feb/2012:13:10:04 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.145.248 - - [29/Feb/2012:13:10:05 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.145.246 - - [29/Feb/2012:13:11:14 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.145.248 - - [29/Feb/2012:13:12:25 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.151.120 - - [29/Feb/2012:13:14:11 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.155.118 - - [29/Feb/2012:13:15:12 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2509 "-" "-" 
66.220.151.120 - - [29/Feb/2012:13:15:59 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.151.123 - - [29/Feb/2012:13:16:01 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.151.121 - - [29/Feb/2012:13:24:43 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.151.120 - - [29/Feb/2012:13:25:22 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.145.248 - - [29/Feb/2012:13:25:51 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.151.121 - - [29/Feb/2012:13:26:14 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 
66.220.145.246 - - [29/Feb/2012:13:26:27 +0530] "POST /fb_notifications/ HTTP/1.1" 403 2528 "-" "-" 

不知道如何做到这一点....请帮助

回答

1

根据日志条目的Facebook订阅回调得到状态403 Forbidden并且这发生在您的代码运行之前。

确保您的应用程序能够达到/fb_subscriptions且代码正在运行。

您可能需要使用你的Web服务器的增加日志级别,为什么请求被阻塞,以获得更多的详细信息...

+1

非常感谢! 我没有注意到它扔403(禁止)..... 这是因为CSRF .... 所以为了避免这种错误在python ....使用@csrf_exempt装饰.... – 2012-02-29 13:43:23