JAVA版微信公众号获取openId,微信用户信息

一. 前端页面js

 1.获取页面传来的数据

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

2.引用

var code = GetQueryString("code");

3.准备

(1).对地址进行UrlEncode编码

var url = encodeURIComponent("http://hamiwangluo.cn/hdw/web/index.html");
// 注意(1): 如果有业务需求,防止页面刷新,业务数据丢失,可以在连接后加参
// 例如:"http://hamiwangluo.cn/hdw/web/index.html?username=wanglei"
// 注意(2):此处的url不加 www、端口    

(2).微信公众号的appid

     微信公众平台登录

(3).微信API

window.location.href ="https://open.weixin.qq.com/connect/oauth2/authorize?" +
            "appid=APPID&redirect_uri=URL&response_type=code" +
            "&scope=snsapi_userinfo&state=STATE#wechat_redirect";

     注意: APPID(大写) 替换为公众号appid、URL(大写) 替换为 上述(1)的url.


4.获取code

getCode();

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

function getCode(){
    var code = GetQueryString("code");
    if(code==null||code==""){
        var url = encodeURIComponent("http://hamikeji.cn/hdw/web/index.html);
        window.location.href ="https://open.weixin.qq.com/c
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
根据提供的引用内容,Java代码中的getopenid方法是用于获取微信公众号openid的。该方法接收一个名为code的参数,通过调用微信API来获取openid。具体的步骤如下: 1. 首先,需要设置公众号的appid和secret。 2. 然后,设置响应头,允许跨域请求。 3. 构建微信登录的URL,包括appid、secret、code和授权类型等参数。 4. 发送GET请求到微信登录URL,获取返回的JSON字符串。 5. 解析JSON字符串,提取其中的openid字段作为结果。 6. 最后,将openid返回。 根据提供的代码,无法确定具体的错误原因。但是根据错误代码40029,这可能是由于code参数无效或过期导致的。建议检查传递给getopenid方法的code参数是否正确,并确保它是最新的。 请注意,以上回答是基于提供的引用内容,可能不完整或有遗漏。如果需要更详细的帮助,请提供更多相关的代码或信息。 #### 引用[.reference_title] - *1* *2* [微信公众号获取openidjava后端+html实现)](https://blog.csdn.net/weixin_43767744/article/details/121747536)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [微信公众号根据openid获取unionid](https://blog.csdn.net/qq_39418742/article/details/126197895)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值