時間:2022-08-06 19:48:01 | 來源:網(wǎng)站運營
時間:2022-08-06 19:48:01 來源:網(wǎng)站運營
最近在接觸微信支付開發(fā),要進行微信支付就需要用戶的唯一標(biāo)識——openid,因為第一次接觸踩了很多坑,于是就把他記錄下來,也便于以后查閱,也方便有需要的人進行參考
openid是微信用戶在公眾號appid下的唯一用戶標(biāo)識(appid不同,則獲取到的openid就不同),可用于永久標(biāo)記一個用戶,同時也是微信JSAPI支付的必傳參數(shù)。
natapp.exe
的文件,雙擊運行 natapp -authtoken 4526sdfe44
,注意 4526sdfe44 是你自己的 authtoken,在 我的隧道 列表中可以看到 authtoken,上文 第3點 中有說到 注意:不要 點擊復(fù)制 按鈕,沒有效果 http://kxfmfu.natappfree.cc -> 127.0.0.1:8080#就是我們需要使用的域名http://kxfmfu.natappfree.cc #本地IP和端口127.0.0.1:8080
免費隧道域名是系統(tǒng)隨機分配,可以的話,花9塊錢買一個固定的隧道也是可以的,有效期是一個月 /** * @program: weixin_demo * @ClassName WeiXinController * @description: * @author: lyy * @create: 2019-11-12 10:16 * @Version 1.0 **/@RestController@RequestMapping("weixin")@Slf4jpublic class WeiXinController { @GetMapping("test") public String auth(){ log.info("test進來了。。。"); return "test測試"; }}
訪問成功: 開發(fā)者需要先到公眾平臺官網(wǎng)中的“開發(fā) - 接口權(quán)限 - 網(wǎng)頁服務(wù) - 網(wǎng)頁帳號 - 網(wǎng)頁授權(quán)獲取用戶基本信息”的配置選項中,修改授權(quán)回調(diào)域名。請注意,這里填寫的是域名(是一個字符串),而不是URL,因此請勿加 http:// 等協(xié)議頭;
也就是說在微信公眾號請求用戶網(wǎng)頁授權(quán)之前,也就是微信開發(fā)之前,填入我們所要開發(fā)的域名,這里我們使用的是測試環(huán)境,所以需要在測試賬號管理頁面 網(wǎng)頁帳號 > 網(wǎng)頁授權(quán)獲取用戶基本信息 填入我們自己的域名,這里要注意填入域名的規(guī)則,這里填寫的是域名(是一個字符串),而不是URL,因此請勿加 http:// 等協(xié)議頭 # 若提示“該鏈接無法訪問”,請檢查參數(shù)是否填寫錯誤,是否擁有scope參數(shù)對應(yīng)的授權(quán)作用域權(quán)限。https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
參數(shù)說明: snsapi_base
和 snsapi_userinfo
兩種,因為我們需要拿到用戶的信息(openid)所以使用 snsapi_userinfo/** * @program: weixin_demo * @ClassName WeiXinController * @description: * @author: lyy * @create: 2019-11-12 10:16 * @Version 1.0 **/@RestController@RequestMapping("weixin")public class WeiXinController { @GetMapping("test") public String auth(@RequestParam("code") String code){ System.out.println(code); return code; }}
2、編輯連接:https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的AppID&redirect_uri=http://域名/weixin/test&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.5.0</version></dependency>
package com.weixin.controller;import com.weixin.config.WeixinUrlConfig;import com.weixin.enums.ResultEnum;import com.weixin.exception.WeixinException;import lombok.extern.slf4j.Slf4j;import me.chanjar.weixin.common.api.WxConsts;import me.chanjar.weixin.common.error.WxErrorException;import me.chanjar.weixin.mp.api.WxMpService;import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import java.net.URLEncoder;/** * @program: sell * @ClassName WeChatController * @description: * @author: lyy * @create: 2019-11-12 21:37 * @Version 1.0 **/@Controller@RequestMapping("wechat")@Slf4jpublic class WeChatController { @Autowired private WxMpService wxMpService; @Autowired private WeixinUrlConfig weixinUrlConfig; @GetMapping("/authorize") public String authorize(@RequestParam("returnUrl") String returnUrl){ //1. 配置 //2. 調(diào)用方法 String url = weixinUrlConfig.getWechatMpAuthorize()+"/wechat/userInfo"; String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(returnUrl)); log.info("【微信網(wǎng)頁授權(quán)獲取code,result={}】",redirectUrl); return "redirect:"+redirectUrl; } @GetMapping("/userInfo") public String userInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) { log.info("進入userInfo信息表里面"); WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken(); try { wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code); } catch (WxErrorException e) { log.error("【微信網(wǎng)頁授權(quán)】{}", e); throw new WeixinException(ResultEnum.WECHAT_MP_ERROR.getCode(), e.getError().getErrorMsg()); } String openId = wxMpOAuth2AccessToken.getOpenId(); return "redirect:" + returnUrl + "?openid=" + openId; }}
WeixinUrlConfigpackage com.weixin.config;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * @program: sell * @ClassName ProjectUrlConfig * @description: * @author: lyy * @create: 2019-11-12 21:41 * @Version 1.0 **/@Data@Component@ConfigurationProperties(prefix = "projecturl")public class WeixinUrlConfig { /** * 微信公眾平臺授權(quán)url */ public String wechatMpAuthorize;}
WechatMpConfigpackage com.weixin.config;import me.chanjar.weixin.mp.api.WxMpService;import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;import me.chanjar.weixin.mp.config.WxMpConfigStorage;import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;/** * @program: sell * @ClassName ProjectUrlConfig * @description: * @author: lyy * @create: 2019-11-12 21:41 * @Version 1.0 **/@Componentpublic class WechatMpConfig { @Autowired private WechatAccountConfig accountConfig; @Bean public WxMpService wxMpService(){ System.out.println("wxMpService"); WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); return wxMpService; } @Bean public WxMpConfigStorage wxMpConfigStorage(){ System.out.println("wxMpConfigStorage(),{}"+accountConfig.getMpAppId()); WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl(); wxMpConfigStorage.setAppId(accountConfig.getMpAppId()); wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret()); return wxMpConfigStorage; }}
配置文件:application.yml 注意:這里的配置文件需要填你自己的信息wechat: mpAppId: wxbd885se5e5se53 mpAppSecret: 3a6bsdf85sdf5wesd5fwesd5fwesdfdeprojecturl: wechatMpAuthorize: http://xxyyg.n52y00.top
http://域名/wechat/authorize?returnUrl=https://www.baidu.com/
www.baidu.com
,點擊復(fù)制鏈接地址,我們就可以看到我們帶過來的 openId 了https://www.baidu.com/?openid=oMU7C1246578976kCEsrAcE
關(guān)鍵詞:授權(quán),獲取,用戶,免費
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。