五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(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)遵守國家法律
青州市| 二连浩特市| 达州市| 无锡市| 太康县| 五河县| 清水河县| 铜川市| 丹寨县| 株洲县| 桃源县| 福清市| 读书| 河池市| 林口县| 碌曲县| 内江市| 遂昌县| 桑日县| 安徽省| 临朐县| 揭阳市| 彩票| 淄博市| 阿克苏市| 镇安县| 新昌县| 泸州市| 西盟| 莱西市| 周宁县| 玛多县| 日照市| 从江县| 秦皇岛市| 民权县| 自贡市| 吴旗县| 涞源县| 洪雅县| 叙永县|