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

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


PHP代码:
<?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<String, String> sign(String jsapi_ticket, String url) {
        Map<String, String> ret = new HashMap<String, String>();
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String string1;
        String signature = "";


        //注意这里参数名必须全部小写,且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                  "&noncestr=" + nonce_str +
                  "&timestamp=" + 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参考//
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
alert("a");
window.wxfx = {
  conf: {
    title: '分享标题',
    desc: '分享描述', // 分享描述
    link: window.location.href,
    ico: 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png',
  },
  /**
   * 微信分享成功
   * @return {[type]} [description]
   */
  shareSuccess: function() {
        alert("b");
  },
  /**
   * 微信分享失败
   * @return {[type]} [description]
   */
  shareCancel: function() {
        alert("c");
  },
  initShare: function() {
    //微信朋友圈分享
    wx.onMenuShareTimeline({
      title: wxfx.conf.title, // 分享标题
      link: wxfx.conf.link, // 分享链接
      imgUrl: wxfx.conf.ico, // 分享图标
      success: function() {
        wxfx.shareSuccess();
        // 用户确认分享后执行的回调函数
      },
      cancel: function() {
        wxfx.shareCancel();
        // 用户取消分享后执行的回调函数
      }
    });
    //微信朋友分享
    wx.onMenuShareAppMessage({
      title: wxfx.conf.title, // 分享标题
      desc: wxfx.conf.desc,
      link: wxfx.conf.link, // 分享链接
      imgUrl: wxfx.conf.ico, // 分享图标
      type: 'link', // 分享类型,music、video或link,不填默认为link
      dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
      success: function() {
        wxfx.shareSuccess();
        // 用户确认分享后执行的回调函数
      },
      cancel: function() {
        wxfx.shareCancel();
        // 用户取消分享后执行的回调函数
      }
    });
    //qq分享
    wx.onMenuShareQQ({
      title: wxfx.conf.title, // 分享标题
      desc: wxfx.conf.desc, // 分享描述
      link: wxfx.conf.link, // 分享链接
      imgUrl: wxfx.conf.ico, // 分享图标
      success: function() {
        wxfx.shareSuccess();
        // 用户确认分享后执行的回调函数
      },
      cancel: function() {
        wxfx.shareCancel();
        // 用户取消分享后执行的回调函数
      }
    });
    //腾讯微薄分享
    wx.onMenuShareWeibo({
      title: wxfx.conf.title, // 分享标题
      desc: wxfx.conf.desc, // 分享描述
      link: wxfx.conf.link, // 分享链接
      imgUrl: wxfx.conf.ico, // 分享图标
      success: function() {
        wxfx.shareSuccess();
        // 用户确认分享后执行的回调函数
      },
      cancel: function() {
        wxfx.shareCancel();
        // 用户取消分享后执行的回调函数
      }
    });
    //QQ空间分享
    wx.onMenuShareQZone({
      title: wxfx.conf.title, // 分享标题
      desc: wxfx.conf.desc, // 分享描述
      link: wxfx.conf.link, // 分享链接
      imgUrl: wxfx.conf.ico, // 分享图标
      success: function() {
        wxfx.shareSuccess();
        // 用户确认分享后执行的回调函数
      },
      cancel: function() {
        wxfx.shareCancel();
        // 用户取消分享后执行的回调函数
      }
    });
  }
};
//下面开始初始化配置参数
wxfx.conf={
    title: '分享标题1',
    desc: '分享描述1', 
    link: window.location.href,
    ico: '{$logo}',
};
    wx.config({
      debug: true,
      //appId: '{$signPackage["appId"]}',
      //timestamp: {$signPackage["timestamp"]},
      //nonceStr: '{$signPackage["nonceStr"]}',
      //signature: '{$signPackage["signature"]}',
      appId: '${appId}',
      timestamp: '${timestamp}',
      nonceStr: '${nonceStr}',
      signature: '${signature}',
      jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'
      ]
    });
    wx.ready(function() {
      wxfx.initShare();
    });
    wx.error(function(res) {
    });
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值