获取用户openid+OAuth2.0网页授权(PHP版本)+JSSDK认证(java版本)

获取用户openid+OAuth2.0网页授权(PHP版本)+JSSDK认证(java版本)


PHP代码:
//$domain_name = "dlydly.tunnel.qydev.com";
$domain_name = "域名:8089";
$appid = "appid";
$secret = "AppSecret";
$code = $_GET['code'];
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
$res = file_get_contents($get_token_url);
$json_obj = json_decode($res,true);
$od = $json_obj['openid'];
//echo 'openid: '.$od;
$redirect_uri = 'http://'.$domain_name.'/jeewx/zpController.do?goLogoInit&od='.$od;
//echo $redirect_uri;
Header("HTTP/1.1 303 See Other"); 
Header("Location:".$redirect_uri); 
//phpinfo()
?>


参考
http://www.cnblogs.com/kenshinobiy/p/5376297.html
https://zhidao.baidu.com/question/1303627230532721299.html
http://blog.csdn.net/peiyuanxin/article/details/52367869
http://www.cnblogs.com/txw1958/p/weixin71-oauth20.html
http://www.cnblogs.com/jupal/articles/5132518.html


----------------------------------------------------------------


    /**
     * 获取接口访问凭证 
     * @param appid 凭证 
     * @param appsecret 密钥
     * @return
     */
    public static String getAccess_tokenYD(String appid, String appsecret) {
    //凭证获取(GET)
    String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    String requestUrl = token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
    // 发起GET请求获取凭证 
    JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
    String access_token = null;
    if (null != jsonObject) {
    try {
    access_token = jsonObject.getString("access_token");    
    } catch (Exception e) {
    access_token = null;
                    // 获取token失败
    String wrongMessage = "获取token失败 errcode:{} errmsg:{}"+jsonObject.getInt("errcode")+jsonObject.getString("errmsg");
    org.jeecgframework.core.util.LogUtil.info(wrongMessage);
    }
    }
    return access_token;
    }


    /**
     * 调用微信JS接口的临时票据
     * @param access_token 接口访问凭证
     * @return
     */
    public static String getJsApiTicket(String access_token) {
    String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
    String requestUrl = url.replace("ACCESS_TOKEN", access_token);
    // 发起GET请求获取凭证         
    JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
    String ticket = null;
    if (null != jsonObject) {
    try {
    ticket = jsonObject.getString("ticket");
    } catch (Exception e) {
    // 获取ticket失败 
    String wrongMessage = "获取ticket失败 errcode:{} errmsg:{}"+jsonObject.getInt("errcode")+jsonObject.getString("errmsg");
org.jeecgframework.core.util.LogUtil.info(wrongMessage);
    }
    }
    return ticket;
    }


////////////////////////以下微信提供的工具类写法//////////////////////////////////////////
    public static Map sign(String jsapi_ticket, String url) {
        Map ret = new HashMap();
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String string1;
        String signature = "";


        //注意这里参数名必须全部小写,且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                  "&noncestr=" + nonce_str +
                  "×tamp=" + timestamp +
                  "&url=" + url;
        System.out.println(string1);


        try
        {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }


        ret.put("url", url);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);


        return ret;
    }


    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }


    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }


    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
    
//////////////////////URL获取参考//////////////////////////////


private static String getUrl(){
HttpServletRequest request = ServletActionContext.getRequest();     
StringBuffer requestUrl = request.getRequestURL();             
String queryString = request.getQueryString();      
String url = requestUrl +"?"+queryString;
return url;
}


private static String getUrlo(HttpServletRequest request){   
StringBuffer requestUrl = request.getRequestURL();             
String queryString = request.getQueryString();      
String url = requestUrl +"?"+queryString;
return url;
}


///////////////////////////////////////////////////////////////////////////////
//////////////////////jsp中引入js参考//////////////////////////////////////////

你可能感兴趣的:(开发语言)