首页 > 编程语言 >微信小程序获取OpenId

微信小程序获取OpenId

时间:2022-11-20 21:56:01浏览次数:58  
标签:OpenId code res 微信 http 获取 org import response

微信小程序获取OpenId

 

微信小程序获取OpenId

在微信小程序中获取OpenId首先需要获取AppId和Secret。

AppId和Secret获取方法

image-20210901224618570

前端

    wx.login({
      success(res) {
        if (res.code) {
          //向后端发起网络请求
          wx.request({
            url: 'http://127.0.0.1:9090/testopenid',
            data: {
              code: res.code
            },
            success:(response)=>{
              //打印OpenId
              console.log(response.data);
            }
          })
        } else {
          console.log('登录失败!' + res.errMsg)
        }
      }
    })
JAVASCRIPT 折叠 复制 全屏

后端:使用Java语言

package cn.order_api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestOpenId {

    @RequestMapping("/testopenid")
    public String getUserInfo(@RequestParam(name = "code") String code) throws Exception {
        System.out.println("code" + code);
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        url += "?appid=xxxx";//将xxxx改成自己的appid
        url += "&secret=xxxx";//将xxxx改成自己的appSecret
        url += "&js_code=" + code;
        url += "&grant_type=authorization_code";
        url += "&connect_redirect=1";
        String res = null;
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);    //GET方式
        CloseableHttpResponse response = null;
        // 配置信息
        RequestConfig requestConfig = RequestConfig.custom()          // 设置连接超时时间(单位毫秒)
                .setConnectTimeout(5000)                              // 设置请求超时时间(单位毫秒)
                .setConnectionRequestTimeout(5000)             // socket读写超时时间(单位毫秒)
                .setSocketTimeout(5000)                    	   // 设置是否允许重定向(默认为true)
                .setRedirectsEnabled(false).build();           // 将上面的配置信息 运用到这个Get请求里
        httpget.setConfig(requestConfig);                      // 由客户端执行(发送)Get请求
        response = httpClient.execute(httpget);                // 从响应模型中获取响应实体
        HttpEntity responseEntity = response.getEntity();
        System.out.println("响应状态为:" + response.getStatusLine());
        if (responseEntity != null) {
            res = EntityUtils.toString(responseEntity);
            System.out.println("响应内容长度为:" + responseEntity.getContentLength());
            System.out.println("响应内容为:" + res);
        }
        // 释放资源
        if (httpClient != null) {
            httpClient.close();
        }
        if (response != null) {
            response.close();
        }
        JSONObject jo = JSON.parseObject(res);
        String openid = jo.getString("openid");
        System.out.println("openid" + openid);
        return openid;
    }
}

  分类: 微信小程序 好文要顶 关注我 收藏该文 ITFengHua
粉丝 - 0 关注 - 0
    +加关注 0 0       « 上一篇: 浏览器隐藏滚动条并滚动
» 下一篇: Vue2 posted @ 2021-09-01 22:48  ITFengHua  阅读(1507)  评论(0)  编辑  收藏  举报     刷新评论刷新页面返回顶部 登录后才能查看或发表评论,立即 登录 或者 逛逛 博客园首页   【推荐】阿里云金秋云创季,云服务器2核2G低至49.68元/年
【推荐】腾讯自研业务上云,5000万核背后的技术难题破解之道
  <iframe data-google-container-id="1" data-load-complete="true" frameborder="0" height="250" id="google_ads_iframe_/1090369/C1_0" marginheight="0" marginwidth="0" name="google_ads_iframe_/1090369/C1_0" scrolling="no" style="border: 0; vertical-align: bottom" title="3rd party ad content" width="300"></iframe> 编辑推荐:
· 记一次 .NET 某工控图片上传 CPU 爆高分析
· 架构与思维:熔断限流的一些使用场景
· 定制 ASP.NET Core 的身份认证
· 从 WinDbg 角度理解 .NET7 的 AOT 玩法
· 当 xxl-job 遇上 docker → 它晕了,我也乱了!
阅读排行:
· .NET跨平台框架选择之一 - Avalonia UI
· Windows之应用安装程序 —— winget
· dafny : 微软推出的形式化验证语言
· 重学c#系列——动态类型[二十二]
· 在 Solidity 中 ++i 为什么比 i++ 更省 Gas?
 

标签:OpenId,code,res,微信,http,获取,org,import,response
From: https://www.cnblogs.com/fanwenyan/p/16909700.html

相关文章