生成临时二维码
临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期。
首先我们创建一个表,用于记录生成的二维码,表名为QRCode,结构如下:
生成临时二维码的代码可以参考:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}"
Dim
hc As
New HttpClient(CExp(url,Functions.Execute("GetAccessToken")))
Dim
jo As
New
JObject
jo("expire_seconds")
= 86400
'有效时间,单位为秒
jo("action_name")
= "QR_SCENE"
'类型为临时二维码
jo("action_info")
= New
JObject
jo("action_info")("scene")
= New
JObject
jo("action_info")("scene")("scene_id")
= 112
'指定此二维码的场景ID
hc.Content
= jo.ToString()
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
Is Nothing
Then
Dim dr
As DataRow
= DataTables("QRCode").AddNew()
dr("Ticket")
= jo("ticket")
dr("ExpireDate")
= Date.Now
.AddSeconds(jo("expire_seconds"))
dr("Descripttion")
= "二维码说明"
dr.Save()
Else
MessageBox.Show(jo("errcode"))
End
If
二维码生成成功后,会返回一个ticket,通过这个ticket可以获取生成的二维码,格式为:
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=返回的ticket
假定返回的ticket为:
gQGj8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZWo2QjlHY1Y5N1Uxek92b3hvMUoAAgRyzpZYAwSAUQEA
那么可以通过下面的网址,访问生成的ticket,ticket记得进行UrlEncode:
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQGj8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZWo2QjlHY1Y5N1Uxek92b3hvMUoAAgRyzpZYAwSAUQEA
如果要将生成的二维码下载到本机,可以参考代码:
Dim
ticket As
String =
"gQGj8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZWo2QjlHY1Y5N1Uxek92b3hvMUoAAgRyzpZYAwSAUQEA"
Dim
hc As
New HttpClient("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="
& UrlEncode(ticket))
hc.GetFile("c:\data\qrcode.jpg")