Android进阶——安卓接入微信,获取OpenID
来源:互联网 发布:淘宝女童模特 编辑:程序博客网 时间:2024/05/29 21:17
需求:接入微信支付,需要获取 OpenID。
安卓接入微信指南:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417751808&token=&lang=zh_CN
移动应用微信登录开发指南:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317851&token=&lang=zh_CN
微信开放平台上面对大多数步骤都有详细的介绍。但是……,还是自己梳理一下吧。
1.申请AppID。
(微信支付或微信登录等功能需要进行开发者资质认证,准备好300大洋)
2.下载最新SDK。
最新sdk下载https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319167&lang=zh_CN
3.导入jar包,并配置权限。
4.代码实现
① 注册到微信
// 通过WXAPIFactory工厂,获取IWXAPI的实例api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, true);api.handleIntent(getIntent(), this);// 将该app注册到微信api.registerApp(Constants.APP_ID);
② 发送请求
final SendAuth.Req req = new SendAuth.Req();req.scope = "snsapi_userinfo";req.state = "wechat_sdk_demo_test";api.sendReq(req);
③ 接受微信请求(获取code值)
// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法@Overridepublic void onResp(BaseResp resp) { int result = 0; SendAuth.Resp re = ((SendAuth.Resp) resp); String code = re.code; switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: result = R.string.errcode_success; getOpenID(code); break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = R.string.errcode_cancel; break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = R.string.errcode_deny; break; default: result = R.string.errcode_unknown; break; } Toast.makeText(this, result, Toast.LENGTH_LONG).show(); Toast.makeText(this, code, Toast.LENGTH_LONG).show();}
④ 通过code获取access_token,code等数据
private void getOpenID(String code) { // APP_ID和APP_Secret在微信开发平台添加应用的时候会生成,grant_type 用默认的"authorization_code"即可. String urlStr = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+Constants.APP_ID+"&secret="+Constants.APP_Secret+ "&code="+code+"&grant_type=authorization_code"; HttpUtils http = new HttpUtils(); // 设置超时时间 // http.configCurrentHttpCacheExpiry(1000 * 10); http.send(HttpRequest.HttpMethod.GET, urlStr, null, new RequestCallBack<String>() { // 接口回调 @Override public void onSuccess(ResponseInfo<String> info) { System.out.println("返回的json字符串:" + info.result); Toast.makeText(getApplicationContext(), info.result, Toast.LENGTH_SHORT).show(); JSONObject obj = null; try { obj = new JSONObject(info.result); //toast OpenID Toast.makeText(getApplicationContext(), obj.getString("openid"), Toast.LENGTH_LONG).show(); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(com.lidroid.xutils.exception.HttpException e, String s) { } });}
注意事项:
1.下载的SDK一定要是最新的,旧一点的SDK里面在获取code的时候没有 .code属性,比如官方demo中万年不变的sdk就害的我很惨。
2.调试时的应用签名和正式版的应用签名是不一致的,应该区别对待,分别获取。签名生成工具链接。
4 0
- Android进阶——安卓接入微信,获取OpenID
- 亲历H5移动端游戏微信支付接入及那些坑(二)——获取Openid和授权
- 微信获取openid
- 微信获取openid
- 获取微信OpenId
- 获取微信OpenID
- 获取微信openid
- 微信获取openid
- 微信获取openid
- 微信支付接口开发——如何获取openid
- 微信移动应用接入开发, Android 授权微信登录获取openid,unionid等,score参数错误或者没有scope权限
- 微信移动应用接入开发, Android 授权微信登录获取openid,unionid等,score参数错误或者没有scope权限
- java微信获取openid
- .NET获取微信openid
- 微信投票-获取openid
- 获取微信用户OpenID
- php 微信获取openid
- 微信获取nickname、openid
- Android中的几种小窗口实现
- leetcode219---Contains Duplicate II
- ZYNQ HLS图像处理加速总结(二)——Processing System软件部分
- 阿里云云服务器ECS Windows Server 2012 R2 64位标准版PHP环境搭建
- python中的lib的一些安装方法
- Android进阶——安卓接入微信,获取OpenID
- leetcode之Convert Sorted Array to Binary Search Tree
- NSData有值,NSData转换成NSString为nil
- Windows系统下查看JDK的版本和安装路径!
- POJ 1650 Integer Approximation (追赶法求解)
- C++使用libcurl做HttpClient
- jQuery.makeArray() 函数详解
- 输入数据,求平均值,输出比平均值大的数据和位置,并统计个数。
- leetcode之Convert Sorted List to Binary Search Tree