package com.backendsys.modules.sdk.baidu.bce.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class BaiduBceUtil {

    @Value("${baidu.ai.client-id}")
    private String BAIDU_BCE_CLIENT_ID;
    @Value("${baidu.ai.client-secret}")
    private String BAIDU_BCE_CLIENT_SECRET;

    public String getAccessToken() {
        Map<String, Object> params = new HashMap<>();
        params.put("grant_type", "client_credentials");
        params.put("client_id", BAIDU_BCE_CLIENT_ID);
        params.put("client_secret", BAIDU_BCE_CLIENT_SECRET);
        String result = HttpUtil.post("https://aip.baidubce.com/oauth/2.0/token", params);
        JSONObject resultJSON = JSONUtil.parseObj(result);
        return resultJSON.getStr("access_token");
    }

}
