If you want to use the JS-SDK interface for uploading and downloading pictures on WeChat, you first need to verify the permissions through the wx.config interface. The parameters appId, timestamp, nonceStr, signature, and jsApiList need to be assigned values.
wx.config({ debug: false, appId: 'wxe49d******43c1cd', timestamp: timestamp, nonceStr: nonceStr, signature: signature, jsApiList: ["chooseImage","previewImage","uploadImage","downloadImage"] });1. Obtaining access_token Access_token is the globally unique ticket of the official account. The official account needs to use the access_token when calling each interface. The access_token can be obtained by calling this API through AppID and SECRET. AppID and SECRET can be obtained in the developer center page of the WeChat public platform.
def getAccessToken() { def APPID = "" def SECRET = "" URL url = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET") def params = "APPID=" + URLEncoder.encode(APPID, 'UTF-8') + "&SECRET=" + URLEncoder.encode(SECRET, 'UTF-8') HttpURLConnection connection = (HttpURLConnection) url.openConnection() connection.setDoOutput(true) connection.setRequestMethod("POST") connection.outputStream.withWriter { Writer writer -> writer.write params } def response = connection.inputStream.withReader { Reader reader -> reader.text } def accessToken = JSON.parse(response).getAt("access_token") return accessToken }Note: access_token is valid for 7200 seconds, so you must cache the access_token value globally in your own service
2. Get jsapi_ticket
jsapi_ticket is a temporary ticket used by the official account to call the WeChat JS interface, which is obtained through access_token.
def getTicket(String accessToken) { URL url = new URL("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi") def params = "ACCESS_TOKEN=" + URLEncoder.encode(accessToken, 'UTF-8') HttpURLConnection connection = (HttpURLConnection) url.openConnection() connection.setDoOutput(true) connection.setRequestMethod("POST") connection.outputStream.withWriter { Writer writer -> writer.write params } def response = connection.inputStream.withReader { Reader reader -> reader.text } def ticket = JSON.parse(response).getAt("ticket") return ticket }
Note: The validity period of jsapi_ticket is 7200 seconds. Since the number of api calls to obtain jsapi_ticket is very limited, frequent refresh of jsapi_ticket will limit api calls, so you must cache the value of jsapi_ticket globally in your own service
3. Generate signature signature Signature generation rules: The fields involved in the signature include noncestr (random string), valid jsapi_ticket, timestamp (timestamp), and url (the URL of the current web page, excluding # and its following parts). After sorting all the parameters to be signed according to the ASCII code of the field name from small to large, use the URL key-value pair format (ie key1=value1&key2=value2…) to concatenate them into a string string1. Note here that all parameter names are lowercase characters. Encrypt string1 with sha1, and use original values for field names and field values without URL escaping.def wxGetConfigParams() { def access_token = getAccessToken() def jsapi_ticket = getTicket(access_token) def nonceStr = UUID.randomUUID().toString() def time = (System.currentTimeMillis() / 1000).toString() def timestamp = time.substring(0, time.lastIndexOf(".")) def url = request.getRequestURL() def signature = getSignature(jsapi_ticket, nonceStr, timestamp, url.toString()) // EVERYTHING // Cache access_token, jsapi_ticket to global variables // Bring signature, nonceStr, timestamp back to the page }
def getSignature(String jsapi_ticket, String nonce_str, String timestamp, String url) { def string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "×tamp=" + timestamp + "&url=" + url MessageDigest crypt = MessageDigest.getInstance("SHA-1") crypt.reset() crypt.update(string1.getBytes("UTF-8")) byteToHex(crypt.digest()) }
def byteToHex(final byte[] hash) { Formatter formatter = new Formatter() for (byte b :hash) { formatter.format("%02x", b) } formatter.toString() }Note: (1) The noncestr and timestamp used for the signature must be the same as the nonceStr and timestamp in wx.config (2) The url used for the signature must be the full URL of the page calling the JS interface (3) For security reasons, the developer must Implementing the signature logic WeChat JS interface signature verification tool: http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign