微信授权获取code请求openID,js+java

作者:LoveEmperor-王子様


# 微信网页获取用户授权

请见另一版: https://blog.csdn.net/qq_31424825/article/details/80272364


## 背景
* 微信网页利用现有微信公众号信息去获取用户的授权,然后得到用户的openID,得到openID后可以进一步获取用户信息,用户微信名,头像,调用微信统一下单支付等。
* 详情见:[微信公众平台](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842)


##  第一步:用户同意授权,获取code  
* 微信公众账号是否拥有授权作用域(scope参数)的权限,即是否网页授权(在接口权限--网页授权处),参数分为:snsapi_base和snsapi_userinfo  
* 应用授权作用域(scope参数),snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )  
* 请求接口实例:
   * scope为snsapi_base
 ```https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect  ```
   * scope为snsapi_userinfo
 ```https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect  ```


* 参数说明:
  ![](/assets/2018-04-21-01.png)  
* 用户同意授权后,如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。页面会自带上code;我们需要获取页面上的code。
* 代码示例:


  * js部分代码   
 
```javascript 
     //    snsapi_base  snsapi_userinfo 第一步用户授权,跳转目标页,在下一页得到code
     var redirect_urls= encodeURIComponent("http://weixin3.szfangle.com/wxapp/roadOutSide/view/Payment.html");
var urls =  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd6c8775dcf404c1f&redirect_uri="+redirect_urls+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
    ```


    ```javascript
function mygets() {
window.location.href = urls;
}
mygets();
    ```


   ```javascript
   //   目标页(redirect_uri跳转页) 获取code,code来自页面返回,这个需要揣摩,隐藏的坑
    function getQueryString (name){
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;


}

var code = getQueryString('code');
   ```  
   ```javascript
   // 将code通过js传给java处理  appid与secret来自微信公众号,
if(code != null){
var parameters = {};
parameters.appid = "wxd6c8775dcf404c1f";
parameters.secret = "9ed577c3e53428189ecbb1dd9a260cd6";
parameters.code = code;
parameters.grant_type="authorization_code"
$.ajax({
type: 'POST',
data:parameters,
url:"http://weixin3.szfangle.com/wxapp/mobileApi/submitWxcode.fgl?",
success: function(res){
console.log(res)

},error:function(res){
console.log(res)
var resData = eval("("+res+")");
}
});


   ```  
##  第二步:得到code,请求openID  
   ```java
    /*
     * 接受来自js提交的code,以及其他参数。正式项目中AppID等应该从配置文件中读取
     * 获取微信授权access_token openid
     * 这里的url必须这样拼接,这是个坑,否则会报AppID is Miss
     * 这里我们得到了openid,
     */
@ResponseBody
    @RequestMapping(value=Url.SUBMIT_WXCODE_URL)  
    private WxGetCodeAck getWxcode(HttpServletRequest request,HttpServletResponse httpResponse,ModelMap model) throws IOException{
    WxGetCodeAck dto = new WxGetCodeAck();
   
    String appid=request.getParameter("appid");
    String secret=request.getParameter("secret");
    String code=request.getParameter("code"); 
    String grant_type=request.getParameter("grant_type");
     
    String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type="+grant_type+"";
     
String ret = HttpUtils.getInstance().doHttpPost(url, "");

Gson gson = new Gson();
dto = gson.fromJson(ret, WxGetCodeAck.class);
if(dto!=null && dto.getOpenid()!=null){
dto.setMsg("ok");
dto.setRet(0);

}else{
dto.setRet(-100);
dto.setMsg("getOpenid is error");
}      
return dto; 
     
     
    }
   ```  
   
## 第三步:如果我们需要获取用户更多信息,微信名,头像等  
* 前提是scope:snsapi_userinfo,即请求code时,参数设置
* 请求url:```http:GET(请使用https协议) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN```  
  * access_token:网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同,即上一步获取openID是的access_token
  * openid:用户的唯一标识
  * lang:返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
* 返回说明:
  ![](/assets/2018-04-21-02.png)  

## 更多精彩请关注微信公众平台:  [微信公众平台](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842)

猜你喜欢

转载自blog.csdn.net/qq_31424825/article/details/80272324