123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package com.mtgg.utils.voice;
- import lombok.extern.slf4j.Slf4j;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * token的获取类
- * 将apiKey和secretKey换取token,注意有效期保存在expiresAt
- */
- @Slf4j
- public class TokenHolder {
- public static final String ASR_SCOPE = "audio_voice_assistant_get";
- public static final String TTS_SCOPE = "audio_tts_post";
- /**
- * URL , Token的url,http可以改为https
- */
- private static final String URL = "http://openapi.baidu.com/oauth/2.0/token";
- /**
- * asr的权限 scope 是 "audio_voice_assistant_get"
- * tts 的权限 scope 是 "audio_tts_post"
- */
- private String scope;
- /**
- * 网页上申请语音识别应用获取的apiKey
- */
- private String apiKey;
- /**
- * 网页上申请语音识别应用获取的secretKey
- */
- private String secretKey;
- /**
- * 保存访问接口获取的token
- */
- private String token;
- /**
- * 当前的时间戳,毫秒
- */
- private long expiresAt;
- /**
- * @param apiKey 网页上申请语音识别应用获取的apiKey
- * @param secretKey 网页上申请语音识别应用获取的secretKey
- */
- public TokenHolder(String apiKey, String secretKey, String scope) {
- this.apiKey = apiKey;
- this.secretKey = secretKey;
- this.scope = scope;
- }
- /**
- * 获取token,refresh 方法后调用有效
- *
- * @return
- */
- public String getToken() {
- return token;
- }
- /**
- * 获取过期时间,refresh 方法后调用有效
- *
- * @return
- */
- public long getExpiresAt() {
- return expiresAt;
- }
- /**
- * 获取token
- *
- * @return
- * @throws IOException http请求错误
- * @throws DemoException http接口返回不是 200, access_token未获取
- */
- public void refresh() throws IOException, DemoException, JSONException {
- String getTokenURL = URL + "?grant_type=client_credentials"
- + "&client_id=" + ConnUtil.urlEncode(apiKey) + "&client_secret=" + ConnUtil.urlEncode(secretKey);
- // 打印的url出来放到浏览器内可以复现
- log.info("token URL:" + getTokenURL);
- URL urlconn = new URL(getTokenURL);
- HttpURLConnection conn = (HttpURLConnection) urlconn.openConnection();
- conn.setConnectTimeout(5000);
- String result = ConnUtil.getResponseString(conn);
- log.info("Token result json:" + result);
- parseJson(result);
- }
- /**
- * @param result token接口获得的result
- * @throws DemoException
- */
- private void parseJson(String result) throws DemoException, JSONException {
- JSONObject json = new JSONObject(result);
- if (!json.has("access_token")) {
- // 返回没有access_token字段
- throw new DemoException("access_token not obtained, " + result);
- }
- if (!json.has("scope")) {
- // 返回没有scope字段
- throw new DemoException("scope not obtained, " + result);
- }
- if (!json.getString("scope").contains(scope)) {
- throw new DemoException("scope not exist, " + scope + "," + result);
- }
- token = json.getString("access_token");
- expiresAt = System.currentTimeMillis() + json.getLong("expires_in") * 1000;
- }
- }
|