wx 登录

首先需要在微信开发平台获取到微信的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) {

                    }

                }
            }
        });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值