首先需要在微信开发平台获取到微信的APP_ID 和APP_SECRET
public static final String APP_ID = "wx0000000000000000";//wx开头+16位
public static final String APP_SECRET = "00000000000000000000000000000000";//32位
1.发送到微信授权
SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.state = "wechat_sdk_demo_test";
api.sendReq(req);
2.在报名下创建wxapi文件夹并创建WXEntryActivity类(登录分享使用,文件夹文件名不能更改)
在onResp中回调授权结果获取code
package com.xjh.xjhdemo.wxapi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
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 com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xjh.xjhdemo.wx.WXUtils;
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
private static final int RETURN_MSG_TYPE_LOGIN = 1;
private static final int RETURN_MSG_TYPE_SHARE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IWXAPI api = WXAPIFactory.createWXAPI(this, WXUtils.APP_ID, true);
api.handleIntent(getIntent(),this);
finish();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
@Override
public void onReq(BaseReq baseReq) {
}
@Override
public void onResp(BaseResp resp) {
switch (resp.errCode) {
case BaseResp.ErrCode.ERR_AUTH_DENIED:
case BaseResp.ErrCode.ERR_USER_CANCEL:
if (RETURN_MSG_TYPE_SHARE == resp.getType()){
Toast.makeText(this,"分享失败",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
}
break;
case BaseResp.ErrCode.ERR_OK:
switch (resp.getType()) {
case RETURN_MSG_TYPE_LOGIN:
//拿到了微信返回的code,立马再去请求access_token
String code = ((SendAuth.Resp) resp).code;
WXUtils.getWXAccessToken(code);
break;
case RETURN_MSG_TYPE_SHARE:
Toast.makeText(this,"分享成功",Toast.LENGTH_SHORT).show();
finish();
break;
}
break;
}
}
}
3.通过code获取access_token和openid
public static void getWXAccessToken(String code){
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
+"appid="+APP_ID+"&"
+"secret="+APP_SECRET+"&"
+"code="+code+"&"
+"grant_type=authorization_code";
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(20, TimeUnit.MINUTES);
builder.readTimeout(20, TimeUnit.MINUTES);
Request request = new Request.Builder()
.get()
.url(url)
.build();
builder.build().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
String string = response.body().string();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(string);
String access_token = jsonObject.getString("access_token");
String openid = jsonObject.getString("openid");
getWXInfo(access_token,openid);
} catch (JSONException e) {
e.printStackTrace();
}
}else {
}
}
});
}
4.通过access_token和openid获取wx个人信息
public static void getWXInfo(String access_token,String open_id){
String url = "https://api.weixin.qq.com/sns/userinfo?"
+"access_token="+access_token+"&"
+"openid="+open_id;
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(20, TimeUnit.MINUTES);
builder.readTimeout(20, TimeUnit.MINUTES);
Request request = new Request.Builder()
.get()
.url(url)
.build();
builder.build().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call,Response response) throws IOException {
if (response.isSuccessful()){
String string = response.body().string();
Log.i("xie", "string=="+string);
try {
JSONObject jsonObject = new JSONObject(string);
} catch (Exception e) {
}
}
}
});
}