能拿到微信用户的用户名,头像,性别,城市等数据.........
讲下遇到的坑,不注意看官方API的后果。
微信常见问题:
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318060&token=&lang=zh_CN
android 移动接入指南: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417751808&token=&lang=zh_CN
android 移动应用微信登录开发指南:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317851&token=&lang=zh_CN
我就是使用了和开发平台填写不一致的签名,所以一直启动不了微信那个授权界面。还有就是微信关闭了或者也没后台运行,点两下登录,才能掉起微信的授权界面,具体什么原因没深究,好像官方API有解释。
------------------------------------------------------------------------------------------------------------------------------------
看完API,照着android接入指南搭好环境之后,就可以写代码了,微信第三方登录还是比较简单的,官方也说了就三步:
第一步:获得code
注册到微信:这个必须要做
发送请求,掉起微信授权界面:
第二步,获得token
//请求Token private void getAccessToken(String code) { String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GlobalConstant.WX_APPID + "&secret=" + GlobalConstant.WX_SECRET + "&code=" + code + "&grant_type=authorization_code"; VolleyRequest.newStringRequest(Request.Method.GET, url, null, new Response.Listener这样会有一大段的json返回给你,要自己解析,拿出需要的token和openid,在第三步中作为参数发出去() { @Override public void onResponse(JSONObject response) { try { access_token = response.getString("access_token"); expires_in = response.getInt("expires_in"); refresh_token = response.getString("refresh_token"); openid = response.getString("openid"); scope = response.getString("scope"); unionid = response.getString("unionid"); Log.e("ljj", "access_token: " + access_token + "\n" + "expires_in: " + expires_in + "\n" + "refresh_token: " + refresh_token + "\n" + "openid: " + openid + "\n" + "scope: " + scope + "\n" + "unionid: " + unionid); } catch (JSONException e) { e.printStackTrace(); } //发起用户详细信息请求 getUser(access_token, openid); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("ljj", "error: " + error); } }); }
第三步:获得用户详细信息
在第二步中,写了一个方法直接发送了请求
public void getUser(String access_token, String openid) { String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid; Log.e("hg", "onResponse: " + url); VolleyRequest.newStringRequest(Request.Method.GET, url, null, new Response.Listener() { @Override public void onResponse(JSONObject response) { //获取的微信用户信息,有头像,性别,名字,城市等等; Log.e("hg", "onResponse: " + response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("ljj", "error: " + error); } }); }
虽然是新手,以前没写过播客,但CSDN这个博客编辑器,有点难操作,挺浪费时间的,一点都不灵性......
直接上代码吧,一共有三步,除了第一步写在了其他界面,第二步,第三步,均写在了wxapi文件下的WXEntryActivity 这个类里面了,下面这个是这个类的由来,在官方文档 android移动接入指南里面有介绍,要按照他的规范写。
第二步和第三步的源码:
package com.hanfang.anyfit.wxapi; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.hanfang.anyfit.AnyfitApplication; import com.hanfang.anyfit.activity.BaseActivity; import com.hanfang.anyfit.httputil.httpUtils.VolleyRequest; import com.hanfang.anyfit.utils.GlobalConstant; import com.tencent.mm.opensdk.modelbase.BaseReq; import com.tencent.mm.opensdk.modelbase.BaseResp; import com.tencent.mm.opensdk.modelmsg.SendAuth; import com.tencent.mm.opensdk.openapi.IWXAPI; import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; import org.json.JSONException; import org.json.JSONObject; /** * Created by Administrator on 2017/6/18. */ public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler { private IWXAPI api; static String access_token; static int expires_in; static String refresh_token; static String openid; static String scope; static String unionid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar(); api = AnyfitApplication.mApp.api; api.handleIntent(getIntent(), this); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); } //微信发送请求到第三方应用,会用到该方法 @Override public void onReq(BaseReq baseReq) { } //第三方应用发送到微信的请求处理后的响应结果,会回调到该方法 @Override public void onResp(BaseResp baseResp) { switch (baseResp.errCode) { case BaseResp.ErrCode.ERR_OK: Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show(); String code = ((SendAuth.Resp) baseResp).code; Log.e("ljj", "wx:" + code); //请求Token getAccessToken(code); finish(); break; case BaseResp.ErrCode.ERR_AUTH_DENIED: Toast.makeText(this, "发送被拒绝", Toast.LENGTH_SHORT).show(); break; case BaseResp.ErrCode.ERR_USER_CANCEL: Toast.makeText(this, "发送取消", Toast.LENGTH_SHORT).show(); break; case BaseResp.ErrCode.ERR_UNSUPPORT: Toast.makeText(this, "不支持错误", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(this, "发送返回", Toast.LENGTH_SHORT).show(); break; } } //请求Token private void getAccessToken(String code) { String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GlobalConstant.WX_APPID + "&secret=" + GlobalConstant.WX_SECRET + "&code=" + code + "&grant_type=authorization_code"; VolleyRequest.newStringRequest(Request.Method.GET, url, null, new Response.Listener() { @Override public void onResponse(JSONObject response) { try { access_token = response.getString("access_token"); expires_in = response.getInt("expires_in"); refresh_token = response.getString("refresh_token"); openid = response.getString("openid"); scope = response.getString("scope"); unionid = response.getString("unionid"); Log.e("ljj", "access_token: " + access_token + "\n" + "expires_in: " + expires_in + "\n" + "refresh_token: " + refresh_token + "\n" + "openid: " + openid + "\n" + "scope: " + scope + "\n" + "unionid: " + unionid); } catch (JSONException e) { e.printStackTrace(); } getUser(access_token, openid); Log.e("ljj", "onResponse: " + response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("ljj", "error: " + error); } }); } public void getUser(String access_token, String openid) { String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid; Log.e("hg", "onResponse: " + url); VolleyRequest.newStringRequest(Request.Method.GET, url, null, new Response.Listener () { @Override public void onResponse(JSONObject response) { //获取的微信用户信息,有头像,性别,名字,城市等等; Log.e("hg", "onResponse: " + response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("ljj", "error: " + error); } }); } }
新手上路,有哪里不对的,望指出。