2012-02-29 79 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