微信小程序获取微信步数

记录一下开发的一下小功能,微信小程序获取微信步数。本来是一个简单的api调用,但是和后端联调的时候一直获取不到。报错-41003 解密失败

获取微信步数解密失败问题

前面我是直接将wx.login的code、iv、encryptedData一起给后端的,但是后端一直报解密失败。我看文档说要先登录。我就让后端再写个获取sessionkey接口给我,我直接sessionkey、iv、encryptedData传给解密的接口。这样修改就好了。

微信小程序获取微信步数使用方法

1.先使用login登录方法获取sessionkey

2.再使用getWeRunData将 encryptedData、iv以及前面后端传给我们的sessionkey,让后端解密就可以了

getstp(){
				wx.login({
					success(e) {
						getSessionKeyAPI({code:e.code}).then(res=>{
							console.log("res",res)
							wx.getWeRunData({
								success(result) {
									var data={
										session_key:res.session_key,
										iv:result.iv,
										encryptedData:result.encryptedData
									}
									getRunDataAPI(data).then(results=>{
										console.log("results",results)
										
									})
								}
							})
						})
						
					}
				})
			}

我们使用stepInfoList这样就获得到了31条数据,第31条是今天的步数

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
微信小程序是一款非常流行的移动应用程序,它提供了许多功能和API供开发者使用。获取微信运动步数是其中的一个功能,以下是一个使用Java编写的微信小程序获取微信运动步数的实例代码。 ```java import java.net.URL; import java.net.HttpURLConnection; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; public class WeChatMiniProgram { public static void main(String[] args) { // 小程序的AppID String appId = "your_app_id"; // 登录凭证code String code = "your_code"; // 小程序的AppSecret String appSecret = "your_app_secret"; // 获取access_token的URL String accessTokenUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&grant_type=authorization_code&js_code=" + code; try { // 发送HTTP请求获取access_token URL url = new URL(accessTokenUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); // 读取返回的数据 InputStream inputStream = connection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder response = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { response.append(line); } // 解析返回的数据 String jsonString = response.toString(); // 提取access_token和openid String accessToken = // 从jsonString中提取access_token String openId = // 从jsonString中提取openId // 获取微信运动步数的URL String stepCountUrl = "https://api.weixin.qq.com/sns/more_info?access_token=" + accessToken + "&openid=" + openId; // 发送HTTP请求获取微信运动步数 URL stepUrl = new URL(stepCountUrl); HttpURLConnection stepConnection = (HttpURLConnection) stepUrl.openConnection(); stepConnection.setRequestMethod("GET"); stepConnection.connect(); // 读取返回的数据 InputStream stepInputStream = stepConnection.getInputStream(); InputStreamReader stepInputStreamReader = new InputStreamReader(stepInputStream, "UTF-8"); BufferedReader stepBufferedReader = new BufferedReader(stepInputStreamReader); StringBuilder stepResponse = new StringBuilder(); String stepLine; while ((stepLine = stepBufferedReader.readLine()) != null) { stepResponse.append(stepLine); } // 解析返回的数据 String stepJsonString = stepResponse.toString(); // 提取微信运动步数 int stepCount = // 从stepJsonString中提取微信运动步数 // 输出微信运动步数 System.out.println("微信运动步数:" + stepCount); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码中,通过发送HTTP请求获取微信小程序的`access_token`和`openId`,然后使用`access_token`和`openId`来发送第二个HTTP请求,获取微信运动步数。需要注意的是,你需要将代码中的`your_app_id`、`your_code`和`your_app_secret`替换为你自己的实际值。此外,代码中的JSON解析部分需要进行具体的实现。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三线码工

码字不易,有钱打赏,没打钱点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值