微信网页授权,解决REDIRECT_URL携带#号,微信返回时临时code等参数拼接在URL中间,而不是最后面

12 篇文章 0 订阅
9 篇文章 0 订阅
  • 微信授权获取临时code的URL
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=012#wechat_redirect
  • 授权逻辑代码
    通过window.history.pushState({}, 0, jumpURL)解决问题=,其中jumpURL为授权界面的URL
const h5AppId = '*******************************';
this.loginParamsH5.appId = h5AppId;

//解析URL的参数,获取临时code
let paramsObj = this.$mHelper.getURLParams(window.location.search);
console.log('当前地址的URL参数=====>', paramsObj);

// 应用的授权界面的URL,携带#号
const jumpURL = 'http://*******/mpshop#/pages/public/wechat-auth';
if (!paramsObj.code) {
	//code作为换取access_token的票据,code只能使用一次,5分钟未被使用自动过期
	const redirectUrl = this.$mConstDataConfig.oauth2URL.replace("APPID", h5AppId).replace("REDIRECT_URI", encodeURIComponent(jumpURL));
	
	//重定向回来,这里需要处理一下URL的参数
	//微信重定向回来的URL:http://*******?code=****&state=***/mpshop#/pages/public/wechat-auth
	//需要修改得到的的URL:http://*******/mpshop#/pages/public/wechat-auth?code=****&state=***
	window.location.href = redirectUrl;
} else {
	this.loginParamsH5.code = paramsObj.code;
	this.loginParamsH5.state = paramsObj.state || '012';
	//下面通过window.history.pushState解决问题
	//const _url = `${window.location.protocol}//${window.location.host}/mpshop#/pages/public/wechat-auth`;
	window.history.pushState({}, 0, jumpURL);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue微信扫码登录出现redirect_uri参数错误,这个错误通常是由于Oauth授权域名没有填写导致的。你可以参考中的解决方法来修复这个问题。具体来说,你需要在微信开发者平台中的授权域名中填写你所使用的域名。这样,当用户点击登录按钮并同意授权后,就可以正确获取用户的OpenId等信息了。另外,你还可以参考中关于微信公众二次开发和配置过程的详细说明。记得在解决后端服务问题后,确保看到正确的授权框。希望对你有所帮助。 参考文献: redirect_uri参数错误的解决办法:https://blog.csdn.net/zl544434558/article/details/46785565 解决微信OAuth2.0网页授权只能设置一个回调域名的问题:https://github.com/HADB/GetWeixinCode 微信公众网页授权:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html 微信公众平台接口调试工具:https://mp.weixin.qq.com/debug<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [redirect_uri参数错误的解决方法(必看)](https://download.csdn.net/download/weixin_38748580/13720294)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [微信公众授权登录后报redirect_uri参数错误的问题](https://blog.csdn.net/sD7O95O/article/details/126092778)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值