Last active
December 10, 2015 17:08
-
-
Save wsky/4465611 to your computer and use it in GitHub Desktop.
Taobao Top SDK Demo v2.0 via language binding to CPP, Bash, and so on.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#how to: . call.sh | |
APPKEY="" | |
SECRET="" | |
SESSION="null" | |
METHOD="taobao.tmc.messages.consume" | |
PARAMETERS_APP=( | |
"group_name=default" | |
"quantity=10" | |
) | |
call_api $APPKEY $SECRET $SESSION $METHOD $URL $IP $SSL ${PARAMETERS_APP[@]} | |
call_api() { | |
local APPKEY=$1 | |
local SECRET=$2 | |
local SESSION=$3 | |
local METHOD=$4 | |
local URL=$5 | |
local IP=$6 | |
local SSL=$7 | |
shift | |
shift | |
shift | |
shift | |
shift | |
shift | |
shift | |
local PARAMETERS_APP=("$@") | |
##################################################################################### | |
local PARAMETERS_BASE=( | |
"method=$METHOD" | |
"v=2.0" | |
"app_key=$APPKEY" | |
"timestamp=$(date +"%s")" | |
"format=json" | |
"partner_id=sdk-bash" | |
) | |
if [ $SESSION == "null" ] | |
then | |
echo "no session" | |
else | |
PARAMETERS_BASE=( | |
"${PARAMETERS_BASE[@]}" | |
"session=$SESSION" | |
) | |
fi | |
local PARAMETERS=( | |
"${PARAMETERS_APP[@]}" | |
"${PARAMETERS_BASE[@]}" | |
) | |
#http://www.microbrew.org/tools/md5sha1sum/md5sha1sum-0.9.5.tar.gz | |
#md5 sign | |
PARAMETERS=($(printf '%s\n' "${PARAMETERS[@]}"|sort)) | |
local SIGN=$SECRET | |
for a in "${PARAMETERS[@]}" | |
do | |
KEY="${a%%=*}" | |
VALUE="${a##*=}" | |
printf "%s=%s\n" "$KEY" "$VALUE" | |
SIGN=$SIGN$KEY$VALUE | |
done | |
#old | |
#SIGN=$SIGN$SECRET | |
echo $SIGN | |
SIGN="$(echo -n "$SIGN"|md5sum|awk '{print $1}')" | |
SIGN="$(echo "$SIGN"|awk '{print toupper($0)}')" | |
echo $SIGN | |
PARAMETERS=( | |
"${PARAMETERS[@]}" | |
"sign=$SIGN" | |
) | |
local QUERY="" | |
for a in "${PARAMETERS[@]}" | |
do | |
QUERY="$QUERY&$a" | |
done | |
QUERY="$(echo "$QUERY"| cut -c 2-)" | |
echo $QUERY | |
local URL="$URL?$QUERY" | |
echo "" | |
echo $URL | |
echo "" | |
curl -H "x-real-ip:$IP" -H "Top-LB-From:$SSL" "$URL" | |
echo "" | |
echo "" | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
namespace SdkDemo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string appkey = ""; | |
string secret = ""; | |
string mix_secret = MD5Encrypt(secret); | |
string refresh_token = ""; | |
string access_token = ""; | |
string redirect_uri = "http://www.xxx.com"; | |
string userid = ""; | |
string code = null; | |
TopClientSdk.Authorize(appkey, mix_secret, code, AuthCallback, null); | |
TopClientSdk.Refresh(appkey, mix_secret, refresh_token, redirect_uri, code, RefreshCallback, null); | |
IDictionary<string, string> dict = PrepareParams(appkey | |
, secret | |
, access_token | |
, new string[] { "method", "fields" } | |
, new string[] { "taobao.user.get", "user_id,uid,nick,sex,buyer_credit,seller_credit,location,created,last_visit,birthday,type,status,alipay_no,alipay_account,alipay_account,email,consumer_protection,alipay_bind" }); | |
string[] p_names = new string[dict.Count]; | |
string[] p_values = new string[dict.Count]; | |
dict.Keys.CopyTo(p_names, 0); | |
dict.Values.CopyTo(p_values, 0); | |
TopClientSdk.Call(appkey, mix_secret, access_token | |
, p_names | |
, p_values | |
, p_names.Length | |
, userid | |
, code | |
, TopCallback | |
, null); | |
} | |
private static void AuthCallback(int ret, IntPtr pData, int iDataLen, object pContext) | |
{ | |
Console.WriteLine("AuthCallback:{0}|{1}", ret, Marshal.PtrToStringAnsi(pData, iDataLen)); | |
} | |
private static void RefreshCallback(int ret, IntPtr pData, int iDataLen, object pContext) | |
{ | |
Console.WriteLine("RefreshCallback:{0}|{1}", ret, Marshal.PtrToStringAnsi(pData, iDataLen)); | |
} | |
private static void TopCallback(int ret, IntPtr pData, int iDataLen, object pContext) | |
{ | |
byte[] buffer = new byte[iDataLen]; | |
Marshal.Copy(pData, buffer, 0, iDataLen); | |
Console.WriteLine("TopCallback:{0}|{1}", ret, Encoding.UTF8.GetString(buffer)); | |
} | |
private static string MD5Encrypt(string originalString) | |
{ | |
var bytes = System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(originalString)); | |
var strb = new StringBuilder(); | |
for (int i = 0; i < bytes.Length; i++) | |
strb.Append(bytes[i].ToString("X2")); | |
return strb.ToString(); | |
} | |
private static IDictionary<string, string> PrepareParams(string appkey | |
, string secret | |
, string sessionkey | |
, string[] biz_p_names | |
, string[] biz_p_values) | |
{ | |
SortedDictionary<string, string> dict = new SortedDictionary<string, string>(); | |
for (var i = 0; i < biz_p_names.Length; i++) | |
dict.Add(biz_p_names[i], biz_p_values[i]); | |
GenerateProtocolParameters(dict, appkey, secret, sessionkey); | |
return dict; | |
} | |
private static void GenerateProtocolParameters(SortedDictionary<string, string> dict | |
, string appkey | |
, string secret | |
, string sessionkey) | |
{ | |
dict.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); | |
dict.Add("v", "2.0"); | |
dict.Add("app_key", appkey); | |
dict.Add("partner_id", "top-windows-sdk"); | |
dict.Add("format", "json"); | |
dict.Add("session", sessionkey); | |
dict.Add("sign_method", "md5"); | |
dict.Add("sign", SignTopParams(secret, dict)); | |
} | |
private static string SignTopParams(string secret, SortedDictionary<string, string> dict) | |
{ | |
StringBuilder strb = new StringBuilder(); | |
strb.Append(secret); | |
foreach (var i in dict) | |
{ | |
strb.Append(i.Key); | |
strb.Append(i.Value); | |
} | |
return MD5Encrypt(strb.Append(secret).ToString()); | |
} | |
} | |
public static class TopClientSdk | |
{ | |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] | |
public delegate void Callback(int ret, IntPtr pData, int iDataLen, object pContext); | |
[DllImport("TopClientSdk.dll" | |
, EntryPoint = "authorize" | |
, CharSet = CharSet.Ansi | |
, CallingConvention = CallingConvention.Cdecl)] | |
public static extern int Authorize(string appkey | |
, string mix_secret | |
, string code | |
, Callback auth_callback | |
, object pContext); | |
[DllImport("TopClientSdk.dll" | |
, EntryPoint = "refresh" | |
, CharSet = CharSet.Ansi | |
, CallingConvention = CallingConvention.Cdecl)] | |
public static extern int Refresh(string appkey | |
, string mix_secret | |
, string refresh_token | |
, string redirect_uri | |
, string code | |
, Callback refresh_callback | |
, object pContext); | |
[DllImport("TopClientSdk.dll" | |
, EntryPoint = "call" | |
, CharSet = CharSet.Ansi | |
, CallingConvention = CallingConvention.Cdecl)] | |
public static extern int Call(string appkey | |
, string mix_secret | |
, string access_token | |
, string[] p_names | |
, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 5)]string[] p_values | |
, int param_len | |
, string userid | |
, string code | |
, Callback top_callback | |
, object pContext); | |
} | |
} |
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 5)]
p_value
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
about char* UTF-8
http://manski.net/2012/06/11/pinvoke-tutorial-passing-strings-part-2/
change to wchar_t*