package com.ruoyi.web.controller.app.baidu;

import com.ruoyi.system.domain.BaiduToken;
import okhttp3.*;
import org.json.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

//@RestController
class GetToken {

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    static  String access_token = "24.d6e37cf9b67921a112de974d2a4930c8.2592000.1743216019.282335-115883169";

//    @Scheduled(cron = "0 0 0 */1 * *")
    public static String getToken() throws IOException{

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token?client_id=G1HsVOPNZhQTe1TyPCPz30kg&client_secret=zKrNsDNe9uEMQLpD5zXzXO4CVwT3spEH&grant_type=client_credentials")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();

        BaiduToken baiduToken = GsonUtils.fromJson(response.body().string(), BaiduToken.class);
////        System.out.println(response.body().string());
        access_token = baiduToken.getAccess_token();
//        System.out.println("获取token, token: "+ access_token);

//        return baiduToken.getAccess_token();
        return access_token;
    }

    public static void main(String []args) throws IOException{
        System.out.println(GetToken.access_token);
        System.out.println(getToken());
        System.out.println(GetToken.access_token);
        System.out.println(getToken());
        System.out.println(GetToken.access_token);
        System.out.println(getToken());
        System.out.println(GetToken.access_token);

//        System.out.println(accessToken);
//        MediaType mediaType = MediaType.parse("application/json");
//        RequestBody body = RequestBody.create(mediaType, "");
//        Request request = new Request.Builder()
//                .url("https://aip.baidubce.com/oauth/2.0/token?client_id=G1HsVOPNZhQTe1TyPCPz30kg&client_secret=zKrNsDNe9uEMQLpD5zXzXO4CVwT3spEH&grant_type=client_credentials")
//                .method("POST", body)
//                .addHeader("Content-Type", "application/json")
//                .addHeader("Accept", "application/json")
//                .build();
//        Response response = HTTP_CLIENT.newCall(request).execute();
//        System.out.println(response.body().string());

    }
}
