生成二维码需要先获取 sdk_ticket,获取方法参考: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Login_via_Scan.html
根据官方的文档描述,得到 sdk_ticket 后,计算出签名,然后发起 GET 查询,就可以得到登录的二维码(Base64)。
测试脚本如下:
#!/usr/bin/env zsh
set -e
if [ $# -lt 2 ]; then
echo "appid sdk_ticket [scope] [timestamp] [noncestr]"
exit -1
fi
_appid=$1
_sdk_ticket=$2
_scope="snsapi_userinfo"
if [ $# -gt 2 ]; then
_scope=$3
fi
_timestamp=`date '+%s'`
if [ $# -gt 3 ]; then
_timestamp=$4
fi
_noncestr=`uuidgen`
if [ $# -gt 4 ]; then
_noncestr=$5
fi
echo "appid=${_appid}"
echo "scope=${_scope}"
echo "noncestr=${_noncestr}"
echo "sdk_ticket=${_sdk_ticket}"
echo "timestamp=${_timestamp}"
_signature=(`echo -n "appid=${_appid}&noncestr=${_noncestr}&sdk_ticket=${_sdk_ticket}×tamp=${_timestamp}" | shasum`)
_signature=${_signature[1]}
echo "signature=${_signature}"
_url="https://open.weixin.qq.com/connect/sdk/qrconnect?appid=${_appid}&noncestr=${_noncestr}×tamp=${_timestamp}&scope=${_scope}&signature=${_signature}"
echo "GET ${_url}"
curl ${_url}