国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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)遵守國家法律
西城区| 洞头县| 闸北区| 汕尾市| 雷波县| 永寿县| 商南县| 温宿县| 手游| 延长县| 长兴县| 漳浦县| 吉水县| 吴旗县| 抚远县| 台江县| 望江县| 邳州市| 武冈市| 汕头市| 黎城县| 保亭| 昌江| 菏泽市| 洞口县| 阳朔县| 诏安县| 怀仁县| 伊春市| 翁源县| 阳城县| 新沂市| 湖州市| 明光市| 静宁县| 徐闻县| 山东| 营山县| 北海市| 电白县| 华池县|