翰林优商.apk(点击下载) / AuthService.java


package com.baidujieru;

import com.baidu.idl.face.api.manager.LogicConst;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;

public class AuthService {
    public static void main(String[] strArr) {
    }

    public static String getAuth() {
        return getAuth("百度云应用的AK", "百度云应用的SK");
    }

    public static String getAuth(String str, String str2) {
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("https://aip.baidubce.com/oauth/2.0/token?" + "grant_type=client_credentials&client_id=" + str + "&client_secret=" + str2).openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.connect();
            Map<String, List<String>> headerFields = httpURLConnection.getHeaderFields();
            for (String str3 : headerFields.keySet()) {
                System.err.println(str3 + "--->" + headerFields.get(str3));
            }
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            String str4 = "";
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    str4 = str4 + readLine;
                } else {
                    System.err.println("result:" + str4);
                    return new JSONObject(str4).getString(LogicConst.ACCESSTOKEN);
                }
            }
        } catch (Exception e) {
            System.err.printf("获取token失败!", new Object[0]);
            e.printStackTrace(System.err);
            return "";
        }
    }
}