2015-05-05 53 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>