Skip to content

MP_OAuth2网页授权

可道 edited this page Mar 31, 2023 · 19 revisions

微信网页授权 - 流程时序图(via. nthack)

(如有错误请指教)

构造网页授权url

首先构造网页授权url,然后构成超链接让用户点击:

WxMpService wxMpService = ...;
String url = ...;
wxMpService.getOAuth2Service().buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)

获得access token

当用户同意授权后,会回调所设置的url并把authorization code传过来,然后用这个code获得access token,其中也包含用户的openid等信息

WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);

获得用户基本信息

WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);

刷新access token

WxOAuth2AccessToken = wxMpService.getOAuth2Service().refreshAccessToken(wxOAuth2AccessToken.getRefreshToken());

验证access token

boolean valid = wxMpService.getOAuth2Service().validateAccessToken(wxOAuth2AccessToken);
Clone this wiki locally