2015-05-05 88 views
0

我正在嘗試將我的網站與Ecwid集成,以便我的用戶可以擁有無​​縫的購物體驗。 Ecwid舉例說明了如何使用PHP對有效載荷進行編碼,然後通過JavaScript發送數據。我需要一個Python/Django實現。該Ecwid例子可以在這裏找到:http://api.ecwid.com/#sso-payloadEcwid SSO與django集成

Ecwid例如:

import time, hmac 
from hashlib import sha1 
def ecwid_sso(request): 
    sso_password = "XXXXXXXXXX" 
    message = base64.b64encode("{appId:'bc',userId:'123',profile:{email:'[email protected]'}}") 
    time_stamp = time.time() 
    payload = hmac.new(sso_password, "%s %s" %(message,time_stamp), sha1).hexdigest() 
    template_data = {'message':message,'payload':payload, 'timestamp':time_stamp} 
    return render_to_response("site/ecwid.html", template_data, context_instance=RequestContext(request)) 

HTML/JavaScript的輸出:

<!DOCTYPE html> 
<html> 
<head lang="en"> 
    <meta charset="UTF-8"> 
    <title>Ecwid test</title> 
</head> 
<body> 
<script src="http://app.ecwid.com/script.js?1003"></script> 
<script> 
    var ecwid_sso_profile = '{{ message }} {{ payload }} {{ timestamp }}' ; 
    window.Ecwid.setSsoProfile(ecwid_sso_profile); 
</script> 
</body> 
</html> 
的Ecwid腳本示例的

<?php 
$sso_secret = "TEST"; 
$message = base64_encode("{appId:'123',userId:'234',profile:{email:'[email protected]'}}"); 
$timestamp = time(); 
$hmac = hash_hmac('sha1', "$message $timestamp", $sso_secret); 
echo "<script> var ecwid_sso_profile = '$message $hmac $timestamp' </script>"; 
?> 

我的Python/Django的翻譯

我從Ecwid得到的錯誤是「無法到達商店。請檢查您的互聯網連接。「這顯然不是真的,因爲我可以發送這篇文章。我認爲我很接近,但是,我目前的假設是,我沒有正確地打包我的負載?思考?

回答

1

上面的錯誤是基於作爲浮點返回的時間戳。 Ecwid要求時間戳爲整數格式。我也更清楚地閱讀說明,現在瞭解整個過程如何工作。我重構和代碼的工作原理如下:

將要顯示的店鋪

查看代碼:

from django.shortcuts import render_to_response 
from django.template.context import RequestContext 
import time, hmac, base64 
from hashlib import sha1 
def any_view_showing_ecwid_shopping_pages(request):  
    sso_password = "XXXXXXXX" 
    message = base64.b64encode("{appId:'bc',userId:'234',profile:{email:'[email protected]'}}") 
    time_stamp = int(time.time()) 
    payload = hmac.new(sso_password, "%s %s" %(message,time_stamp), sha1).hexdigest() 
    return render_to_response("site/ecwid.html", {'message':message,'payload':payload, 'timestamp':time_stamp}, 
           context_instance=RequestContext(request)) 

的JavaScript:

<script> 
    var ecwid_sso_profile = '{{ message }} {{ payload }} {{ timestamp }}' ;  
</script>