公眾號code是微信公眾號開發時需要獲取的一個參數,獲取後可以進行用戶信息的獲取和授權。在Java中,獲取公眾號code可以通過以下幾個方面:
一、使用微信網頁授權獲取用戶信息
1、首先需要構造授權頁面的URL,如下:
String appId = "wxAppId";
String redirectUri = "https://yourCallBackUrl.com";
String scope = "snsapi_userinfo";
String state = "YourCustomState";
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
"appid=" + appId +
"&redirect_uri=" + URLEncoder.encode(redirectUri, "UTF-8") +
"&response_type=code" +
"&scope=" + scope +
"&state=" + state +
"#wechat_redirect";
其中,appId為公眾號的唯一標識,redirectUri為用戶授權後的回調頁面,可以設置為自定義的URL。scope是授權的方式,分為snsapi_base和snsapi_userinfo,前者僅獲取用戶的基本信息,後者可以獲取用戶的詳細信息。state是自定義的參數,可以用於防止CSRF攻擊。
2、將構造好的URL返回給前端,用戶點擊授權後會被跳轉到微信授權頁面,根據scope的不同,用戶的授權方式也不同。
3、用戶授權完成後,微信會將一個code參數返回給回調頁面,這個code就是獲取用戶信息所必須的參數。可以通過以下方式獲取code:
String code = request.getParameter("code");
其中,request是授權回調頁面的HttpServletRequest對象。
二、使用微信公眾平台SDK獲取code
1、首先需要引入微信公眾平台SDK,可以通過以下方式引入:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.8.0</version>
</dependency>
2、在代碼中構建授權頁面的URL,並將其重定向到微信授權頁面:
WxMpService wxService = new WxMpServiceImpl();
wxService.setWxMpConfigStorage(configStorage);
String redirectUrl = wxService.oauth2buildAuthorizationUrl("https://yourCallBackUrl.com", "snsapi_userinfo", "YourCustomState");
response.sendRedirect(redirectUrl);
其中,configStorage為WxMpConfigStorage對象,可以通過配置公眾號的appId和appSecret來進行初始化。
3、用戶授權完成後,微信會將一個code參數返回給回調頁面,通過以下方式獲取code:
WxMpOAuth2AccessToken accessToken = wxService.oauth2getAccessToken(code); String openId = accessToken.getOpenId();
其中,openId可以用於獲取用戶的詳細信息。
三、使用Spring Boot集成微信公眾平台SDK獲取code
1、首先需要在pom.xml文件中引入spring-boot-starter-web和weixin-java-mp包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.8.0</version>
</dependency>
2、在application.properties或application.yml文件中配置公眾號的appId和appSecret:
wx.mp.appId=yourAppId wx.mp.secret=yourSecret
3、創建一個Controller,並定義授權的URL,將其返回給前端進行跳轉:
@RestController
public class WxMpController {
@Autowired
private WxMpService wxMpService;
@GetMapping("/wx/authorize")
public void authorize(@RequestParam("state") String state, HttpServletResponse response) throws IOException {
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl("https://yourCallBackUrl.com", "snsapi_userinfo", state);
response.sendRedirect(redirectUrl);
}
}
4、定義授權回調頁面的URL,並處理回調過來的參數:
@RestController
public class WxMpController {
@Autowired
private WxMpService wxMpService;
@GetMapping("/wx/callback")
public void callback(@RequestParam("code") String code,
@RequestParam("state") String state,
HttpServletResponse response) throws IOException {
WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
String openId = accessToken.getOpenId();
// TODO: 獲取用戶信息
response.sendRedirect("https://yourSuccessUrl.com");
}
}
通過以上三個步驟,可以快速地獲取公眾號code,並藉此獲取用戶的詳細信息。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/156663.html
微信掃一掃
支付寶掃一掃