安卓移动端接入扫码登录auth时报错?
发布于 4 年前 作者 xiulan52 4049 次浏览 来自 问答

按照步骤,安卓移动端接入扫码登录,auth报错步骤如下

  //获取token
    private void wechatfirst() {
//        String url = "http://eyetrain.holosz.com/loginC/getKey";
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=我的APPID&secret=我的secret";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).get().build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                WeChatFirst resultdata = gson.fromJson(result, WeChatFirst.class);
                String access_token = resultdata.getAccess_token();

                getLoginQrCode(access_token);

            }
        });
    }

    /**
     * 获取二维码
     * 获取ticket
     */
    private void getLoginQrCode(String token) {
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+token+"&type=2";
//        loginPresenter.getLoginQrCode();
        OkHttpClient okHttpClient = new OkHttpClient();

        /* final Request request = new Request.Builder().url(url).get().build();*/
        final Request request = new Request.Builder()
//                .url("http://eyetrain.holosz.com/loginC/getsdkTicket")
                .url(url)
                .get().build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                Toast.makeText(LoginActivity.this, "网络请求失败,请检查网络", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                TicketBean resultdata = gson.fromJson(result, TicketBean.class);
                String ticket = resultdata.getTicket();//拿到Ticket

                StringBuilder str = new StringBuilder();// 定义变长字符串
                Random random = new Random();
                // 随机生成数字,并添加到字符串
                for (int i = 0; i < 8; i++) {
                    str.append(random.nextInt(10));
                }
                String noncestr = str.toString();
                String timeStamp = new SimpleDateFormat(TIME_FORMAT).format(new Date());
                String string1 = String.format("appid=%s&noncestr=%s&sdk_ticket=%s&timestamp=%s", Constant.WeChatAppID, noncestr, ticket, timeStamp);
                String sha = EncryptUtils.getSHA(string1);
                sign(noncestr, timeStamp, sha);
            }
        });

    }

    //第三步
    private void sign(String noncestr, String timeStamp, String sha) {
        oauth.auth(Constant.WeChatAppID, "snsapi_userinfo", noncestr, timeStamp, sha, this);
    }
 报错如下

回到顶部