移动端微信第三方平台授权

一、获取二维码链接中的uuid,生成授权二维码

        1、通过第三方appid,appsecret,获取第三方平台的access_token。详情看官方文档

        2、通过第三方appid,第三方平台的access_token,获取预授权码。详情看官方文档

        3、获取uuid

            请求地址:https://mp.weixin.qq.com/safe/safeqrconnect

            方法:post

            参数:action: 'bindcomponent',
                       state: 0,
                       component_appid: "第三方平台的appid",
                       component_pre_auth_code: "预授权码",
                       component_redirect_uri: "回调地址"

             返回:{"uuid":"94af28c5c81042eea43da187517c26","genuuid_res":0}
       

 //模拟浏览器请求     
ArrayList<NameValuePair> list = new ArrayList<>();        
NameValuePair action = new NameValuePair("action", "bindcomponent");        
NameValuePair state = new NameValuePair("state", "123");        
NameValuePair appid = new NameValuePair("component_appid", component_appid);        
NameValuePair code = new NameValuePair("component_pre_auth_code", preAuthCode);        
NameValuePair redirect = new NameValuePair("component_redirect_uri",component_redirect_uri);        
list.add(action);list.add(state);list.add(appid);list.add(code);list.add(redirect);       
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("https://mp.weixin.qq.com/safe/safeqrconnect");
//不设置请求头Referer,授权确认时会提示refer错误
postMethod.setRequestHeader("Referer", request.getHeader("Referer"));
NameValuePair[] params = new NameValuePair[list.size()];
for (int i = 0; i < list.size(); i++) {
     params[i] = list.get(i);
}
postMethod.addParameters(params);
client.executeMethod(postMethod);
String resultStr = postMethod.getResponseBodyAsString();

  将返回的uuid,替换UUID,便可以生成授权二维码

   https://mp.weixin.qq.com/safe/safeqrcode?action=bindcomponent&uuid=UUID&auth_type=3

二、查询授权状态

       请求地址:https://mp.weixin.qq.com/safe/safeuuid

       方法:get

       参数:timespam: "当前时间戳",
                  uuid: "生成二维码的uuid",

      返回:{ "errcode":401,  "key":"","pass_ticket":"","card_name":"","check_status":0}  //未扫码

                 { "errcode":402,  "key":"","pass_ticket":"","card_name":"","check_status":0} 

                 { "errcode":404,  "key":"","pass_ticket":"","card_name":"","check_status":0}  //扫完码,但未确定授权

                 { "errcode":500,  "key":"","pass_ticket":"","card_name":"","check_status":0}  //出现错误(二维码过期等等问题)

                 {  "errcode":405,    //确定授权后
                    "key":"",
                    "pass_ticket":"",
                    "card_name":"",
                    "check_status":0,
                     "confirm_resp":{
                                 "redirect_uri":"http:xxxx.com/callback.api?auth_code=AUTH_CODE&expires_in=3600",
                                 "component_status":0,
                                 "component_pre_auth_code":"授权码",
                                 "component_appid":"第三方平台appid",
                                 "bizuin":"MzI0NjQxNTI2MQ==",
                                 "open_component_uin":0,
                                 "open_mp_appid":"",
                                 "open_mp_uin":0,
                                 "open_biz_mp_mchid":0,
                                 "biz_mp_uin":0,
                                 "biz_mp_appid":"",
                                 "biz_mp_mchid":0
        }

三、回调处理

     errorcode 405时,confirm_resp.redirect_uri即回调地址,get请求

    1、通过授权码confirm_resp.auth_code,获取第三方平台access_token

    2、通过第三方平台access_token,便可以获取到授权方的信息

猜你喜欢

转载自blog.csdn.net/qq_32323501/article/details/82906562