无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

前端學(xué)習(xí)日記(1)微信小程序獲取openid問題

2023-07-04 16:16 作者:亂刮  | 我要投稿

首先在wx.login()獲取登錄jscode,然后把a(bǔ)ppid、secret、jscode放入下面接口對(duì)應(yīng)位置

GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

小程序代碼實(shí)際格式應(yīng)該是:

url:`https://api.weixin.qq.com/sns/jscode2session?appid=${APPID}&secret=${SECRET}&js_code=${JSCODE}&grant_type=authorization_code`

(注意使用的是反引號(hào)``,大寫變量名則是前面就定義好的)

訪問接口后返回?cái)?shù)據(jù)中就包含openid和session_key等信息。


然而,在微信小程序調(diào)試時(shí)允許這么訪問,如果小程序要發(fā)布上線,必須把上述訪問過程放到后臺(tái)執(zhí)行。我在網(wǎng)上搜到的springboot后臺(tái)接口代碼:

import java.util.HashMap;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import com.github.kevinsawicki.http.HttpRequest;


@Service

public class wx_jscode2openidServiceImpl implements wx_jscode2openidService{

? ? @Override

? ? public String jscode2openid(String code) {

? ? ? ? Map<String, String> data = new HashMap<String, String>();

? ? ? ? data.put("appid", "你的appid");

? ? ? ? data.put("secret", "你的secret");

? ? ? ? data.put("js_code", code);

? ? ? ? data.put("grant_type", "authorization_code");


? ? ? ? String response = HttpRequest.get("https://api.weixin.qq.com/sns/jscode2session").form(data).body();

? ? ? ? System.out.println("Response was: " + response);

? ? ? ? JSONObject obj= JSON.parseObject(response);//將json字符串轉(zhuǎn)換為json對(duì)

? ? ? ? //System.out.println(obj);


? ? ? ? return obj.toString();

? ? }

}


其中的code即為jscode,由微信小程序端發(fā)送到后臺(tái)。


在springboot的pom文件中需要導(dǎo)入依賴:

<!--http requset-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.github.kevinsawicki</groupId>

? ? ? ? ? ? <artifactId>http-request</artifactId>

? ? ? ? ? ? <version>6.0</version>

? ? ? ? </dependency>


? ? ? ? <!--json 解析-->

? ? ? ? <!--https://mvnrepository.com/artifact/com.alibaba/fastjson -->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.alibaba</groupId>

? ? ? ? ? ? <artifactId>fastjson</artifactId>

? ? ? ? ? ? <version>1.2.49</version>

? ? ? ? </dependency>


小程序發(fā)布后會(huì)提醒“session_key存在明文傳輸”的問題,但暫時(shí)沒發(fā)現(xiàn)影響。


前端學(xué)習(xí)日記(1)微信小程序獲取openid問題的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
莎车县| 曲松县| 门头沟区| 独山县| 抚松县| 区。| 格尔木市| 庆阳市| 雷波县| 铜鼓县| 垣曲县| 阿图什市| 华池县| 上高县| 黑山县| 武夷山市| 阿坝县| 剑河县| 拜城县| 买车| 察哈| 昌江| 佛坪县| 同仁县| 阿克苏市| 西安市| 安塞县| 梁河县| 阳信县| 平塘县| 贡觉县| 冕宁县| 惠来县| 微山县| 青浦区| 肃宁县| 铜川市| 武定县| 嘉黎县| 资兴市| 朝阳县|