public static void main(String[] args) throws IOException {
        String authorization = getAuthorization();
    }
    
    public static String getAuthorization() {
        String username = "AUx-3CDaiFIjT9zxup";
        String password = "EPpmIdV6YKKshrOKYP9gZH";
        String oauthUrl = "https://api-m.san.pay.com/v1/oauth2/token";
        String token = basicAuth(oauthUrl, username, password, "api-m.san.pay.com", 443, "https");

        return "Bearer " + token;
    }

    public static String basicAuth(String url, String username, String password, String hostName, int port, String scheme) {
        String token = null;
        HttpHost target = new HttpHost(hostName, port, scheme);

        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

        credentialsProvider.setCredentials(
                new AuthScope(target.getHostName(), port),
                new UsernamePasswordCredentials(username, password));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credentialsProvider).build();
        try {
            AuthCache authCache = new BasicAuthCache();

            BasicScheme basicAuth = new BasicScheme();
            authCache.put(target, basicAuth);

            HttpClientContext localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);

            HttpPost httppost = new HttpPost(url);
            HttpEntity reqEntity = null;
            JSONObject json = new JSONObject();
            json.put("grant_type", "client_credentials");
            List<NameValuePair> params = toCharge(json);

            if (!params.isEmpty()) reqEntity = new UrlEncodedFormEntity(params, "UTF-8");
            httppost.setEntity(reqEntity);

            try (CloseableHttpResponse response = httpclient.execute(target, httppost, localContext)) {
                HttpEntity entity = response.getEntity();
                System.out.println(entity);
                if (entity != null) {
                    String result = EntityUtils.toString(entity, CharsetUtil.defaultCharset());

                    JSONObject jsonObject = JSONObject.parseObject(result);

                    System.out.println(jsonObject);
                    token = jsonObject.getString("access_token");

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return token;
    }

    public static List<NameValuePair> toCharge(JSONObject params) {
        List<NameValuePair> listParam = new ArrayList<NameValuePair>();
        if (params != null && params.size() > 0) {
            for (Iterator it = params.keySet().iterator(); it.hasNext(); ) { ////这个返回key集合的对象
                String key = (String) it.next();
                if (params.get(key) != null) {
                    listParam.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
                }
            }
        }
        return listParam;
    }

Q.E.D.


行走在天地间自由的灵魂