Ecwid SSO与django集成

时间:2015-05-05 18:27:20

标签: single-sign-on

我正在尝试将我的网站与Ecwid集成,以便我的用户可以获得无缝的购物体验。 Ecwid给出了一个如何使用PHP对有效载荷进行编码然后通过JavaScript发送数据的示例。我需要一个Python / Django实现。可在此处找到Ecwid示例:http://api.ecwid.com/#sso-payload

Ecwid示例:

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

我的Ecwid脚本示例的Python / django翻译:

import time, hmac
from hashlib import sha1
def ecwid_sso(request):
    sso_password = "XXXXXXXXXX"
    message = base64.b64encode("{appId:'bc',userId:'123',profile:{email:'chris@bc.com'}}")
    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收到的错误是“无法到达商店。请检查您的互联网连接。”这显然不正确,因为我可以发送这篇文章。我想我很接近但是,我目前的假设是我没有正确打包我的有效载荷?想法?

1 个答案:

答案 0 :(得分:1)

The error above was based on the timestamp being returned as a float. Ecwid requires the timestamp to be in integer format. I also read the instructions more clearly and now understand how the whole process works. I refactored and the code works as follows:

View code that will be showing the shop:

css

JavaScript:

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:'chris@bc.com'}}")
    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))