TokenHolder.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.mtgg.utils.voice;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.json.JSONException;
  4. import org.json.JSONObject;
  5. import java.io.IOException;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. /**
  9. * token的获取类
  10. * 将apiKey和secretKey换取token,注意有效期保存在expiresAt
  11. */
  12. @Slf4j
  13. public class TokenHolder {
  14. public static final String ASR_SCOPE = "audio_voice_assistant_get";
  15. public static final String TTS_SCOPE = "audio_tts_post";
  16. /**
  17. * URL , Token的url,http可以改为https
  18. */
  19. private static final String URL = "http://openapi.baidu.com/oauth/2.0/token";
  20. /**
  21. * asr的权限 scope 是 "audio_voice_assistant_get"
  22. * tts 的权限 scope 是 "audio_tts_post"
  23. */
  24. private String scope;
  25. /**
  26. * 网页上申请语音识别应用获取的apiKey
  27. */
  28. private String apiKey;
  29. /**
  30. * 网页上申请语音识别应用获取的secretKey
  31. */
  32. private String secretKey;
  33. /**
  34. * 保存访问接口获取的token
  35. */
  36. private String token;
  37. /**
  38. * 当前的时间戳,毫秒
  39. */
  40. private long expiresAt;
  41. /**
  42. * @param apiKey 网页上申请语音识别应用获取的apiKey
  43. * @param secretKey 网页上申请语音识别应用获取的secretKey
  44. */
  45. public TokenHolder(String apiKey, String secretKey, String scope) {
  46. this.apiKey = apiKey;
  47. this.secretKey = secretKey;
  48. this.scope = scope;
  49. }
  50. /**
  51. * 获取token,refresh 方法后调用有效
  52. *
  53. * @return
  54. */
  55. public String getToken() {
  56. return token;
  57. }
  58. /**
  59. * 获取过期时间,refresh 方法后调用有效
  60. *
  61. * @return
  62. */
  63. public long getExpiresAt() {
  64. return expiresAt;
  65. }
  66. /**
  67. * 获取token
  68. *
  69. * @return
  70. * @throws IOException http请求错误
  71. * @throws DemoException http接口返回不是 200, access_token未获取
  72. */
  73. public void refresh() throws IOException, DemoException, JSONException {
  74. String getTokenURL = URL + "?grant_type=client_credentials"
  75. + "&client_id=" + ConnUtil.urlEncode(apiKey) + "&client_secret=" + ConnUtil.urlEncode(secretKey);
  76. // 打印的url出来放到浏览器内可以复现
  77. log.info("token URL:" + getTokenURL);
  78. URL urlconn = new URL(getTokenURL);
  79. HttpURLConnection conn = (HttpURLConnection) urlconn.openConnection();
  80. conn.setConnectTimeout(5000);
  81. String result = ConnUtil.getResponseString(conn);
  82. log.info("Token result json:" + result);
  83. parseJson(result);
  84. }
  85. /**
  86. * @param result token接口获得的result
  87. * @throws DemoException
  88. */
  89. private void parseJson(String result) throws DemoException, JSONException {
  90. JSONObject json = new JSONObject(result);
  91. if (!json.has("access_token")) {
  92. // 返回没有access_token字段
  93. throw new DemoException("access_token not obtained, " + result);
  94. }
  95. if (!json.has("scope")) {
  96. // 返回没有scope字段
  97. throw new DemoException("scope not obtained, " + result);
  98. }
  99. if (!json.getString("scope").contains(scope)) {
  100. throw new DemoException("scope not exist, " + scope + "," + result);
  101. }
  102. token = json.getString("access_token");
  103. expiresAt = System.currentTimeMillis() + json.getLong("expires_in") * 1000;
  104. }
  105. }