🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
富源语音转写是异步任务的方式展开,一个是创建任务,一个是获得任务结果。但在访问这两个接口前需要获得授权 `access_token` ,所以共有三个接口。 # ## 1、获得授权access_token 访问这个接口需要提供`appid`和`secret`,这两者需要到富源平台注册应用,然后在应用的右侧获得。 这个`access_token`的有效时间为两个小时,请注意管理避免过期。 # ~~~[api:notDebug] get:/v1/token *appid=a44r4aMbO7BZ1NoW#申请的appid *secret=a4474wRq9BwujRV7hM4SGH0QMyrEJF5J#申请的appsecret *grant_type=client_credential#类型 <<< success { "accessToken": "a447bdDAUeKORvmt", "expires": 7200 } <<< 403 { "status": 403, "error": "SecretIsInvalid", "message": "无效的 secret" } ~~~ # ##### C#访问示例代码: ~~~CSharp var client = new RestClient("http://gateway.api.yun2win.com/v1/token?grant_type=client_credential&appid=a44r4aMbO7BZ1NoW&secret=a4474wRq9BwujRV7hM4SGH0QMyrEJF5J"); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AlwaysMultipartFormData = true; IRestResponse response = client.Execute(request); Console.WriteLine(response.Content); ~~~ # ##### Java访问示例代码: ~~~ java OkHttpClient client = new OkHttpClient().newBuilder().build(); Request request = new Request.Builder() .url("http://gateway.api.yun2win.com/v1/token?grant_type=client_credential&appid=a44r4aMbO7BZ1NoW&secret=a4474wRq9BwujRV7hM4SGH0QMyrEJF5J") .method("GET", null) .build(); Response response = client.newCall(request).execute(); ~~~ # #### Nodejs访问示例代码: ~~~javascript var unirest = require('unirest'); var req = unirest('GET', 'http://gateway.api.yun2win.com/v1/token?grant_type=client_credential&appid=a44r4aMbO7BZ1NoW&secret=a4474wRq9BwujRV7hM4SGH0QMyrEJF5J') .end(function (res) { if (res.error) throw new Error(res.error); console.log(res.raw_body); }); ~~~ # ## 2、创建语音转写任务 上传已录制好的音频(暂只支持mp3,不能超过10M),生成语音转写任务。任务创建成功,自动会加入转写队列。 # ~~~[api:notDebug] post:/api/lasr/long/create *file:aduio=c:/a.mp3#已录制好的音频 <<< success { "taskId": "a45D16UhKJ4xeSSO", "duration": 3, "status": "处理中" } <<< 401 错误一: { "status": 401, "error": "AccessTokenNoExist", "message": "必须有 accessToken" } 错误二: { "status": 401, "error": "AccessTokenIsInvalid", "message": "无效的 accessToken" } 错误三: { "status": 401, "error": "AccessTokenHasExpired", "message": "accessToken 已过期" } <<< 403 错误一: { "status": 403, "error": "SecretIsInvalid", "message": "无效的 secret" } 错误二: { "status": 403, "error": "SecretHasDisabled", "message": "secret 已被禁用" } 错误三: { "status": 403, "error": "SecretError", "message": "secret 状态异常" } 错误四: { "status": 403, "error": "LimitExceeded", "message": "已超出用量限制" } <<< 500 { "error":"Params_Audio_Missing", "message:"请补充语音文件audio栏位(mp3)" } { "error":"Audio_Len_Too_Short", "message:"音频文件太小,长度不能少于1秒" } ~~~ # ##### C#访问示例代码: ~~~CSharp var client = new RestClient("http://gateway.api.yun2win.com/api/lasr/long/create"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Authorization", "Bearer a447bdDAUeKORvmt"); // 这里的a447bdDAUeKORvmt就是接口1取得的access_token request.AddFile("audio", "/Users/test/Desktop/zv1qh-yfbgq.mp3"); IRestResponse response = client.Execute(request); Console.WriteLine(response.Content); ~~~ # ##### Java访问示例代码: ~~~ java OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM) .addFormDataPart("audio","zv1qh-yfbgq.mp3", RequestBody.create(MediaType.parse("application/octet-stream"), new File("/Users/test/Desktop/zv1qh-yfbgq.mp3"))) .build(); Request request = new Request.Builder() .url("http://gateway.api.yun2win.com/api/lasr/long/create") .method("POST", body) .addHeader("Authorization", "Bearer a447bdDAUeKORvmt") // 这里的a447bdDAUeKORvmt就是接口1取得的access_token .build(); Response response = client.newCall(request).execute(); ~~~ # #### Nodejs访问示例代码 ~~~javascript var unirest = require('unirest'); var req = unirest('POST', 'http://gateway.api.yun2win.com/api/lasr/long/create') .headers({ 'Authorization': 'Bearer a447bdDAUeKORvmt' // 这里的a447bdDAUeKORvmt就是接口1取得的access_token }) .attach('file', '/Users/test/Desktop/zv1qh-yfbgq.mp3') .end(function (res) { if (res.error) throw new Error(res.error); console.log(res.raw_body); }); ~~~ # ## 3、获得语音转写任务的结果 从接口获得了`taskId`,要获得转写结果需要访问下面接口,建议可以不定期请求,请求间隔时间建议请参考最佳实践。 注意taskId是直接写在访问接口地址中,而不是写在query里。 # ~~~[api:notDebug] get:/api/lasr/long/:taskId/result *taskId=a45D16UhKJ4xeSSO#任务Id <<< success { "status": "已完成", "result": [ { "begin": "780", // 此段的开始时间,单位毫秒 "end": "2770", // 此段的结束时间,单位毫秒 "text": "这是做一个测试。" // 此段识别的文字 } ] } { "status": "处理中" } <<< 401 错误一: { "status": 401, "error": "AccessTokenNoExist", "message": "必须有 accessToken" } 错误二: { "status": 401, "error": "AccessTokenIsInvalid", "message": "无效的 accessToken" } 错误三: { "status": 401, "error": "AccessTokenHasExpired", "message": "accessToken 已过期" } <<< 403 错误一: { "status": 403, "error": "SecretIsInvalid", "message": "无效的 secret" } 错误二: { "status": 403, "error": "SecretHasDisabled", "message": "secret 已被禁用" } 错误三: { "status": 403, "error": "SecretError", "message": "secret 状态异常" } 错误四: { "status": 403, "error": "LimitExceeded", "message": "已超出用量限制" } <<< 500 { "error":"Asr_Task_Not_Exist", "message:"语音转换任务不存在,请检查taskId是否正确" } { "error":"Asr_Task_No_Right", "message:"没有权限访问语音转换任务" } ~~~ # ##### C#访问示例代码: ~~~CSharp var client = new RestClient("http://gateway.api.yun2win.com/api/lasr/long/a45D16UhKJ4xeSSO/result"); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("Authorization", "Bearer a447bdDAUeKORvmt"); // 这里的a447bdDAUeKORvmt就是接口1取得的access_token request.AlwaysMultipartFormData = true; IRestResponse response = client.Execute(request); Console.WriteLine(response.Content); ~~~ # ##### Java访问示例代码: ~~~ java OkHttpClient client = new OkHttpClient().newBuilder() .build(); Request request = new Request.Builder() .url("http://gateway.api.yun2win.com/api/lasr/long/a45D16UhKJ4xeSSO/result") .method("GET", null) .addHeader("Authorization", "Bearer a447bdDAUeKORvmt") // 这里的a447bdDAUeKORvmt就是接口1取得的access_token .build(); Response response = client.newCall(request).execute(); ~~~ # #### Nodejs访问示例代码 ~~~javascript var unirest = require('unirest'); var req = unirest('GET', 'http://gateway.api.yun2win.com/api/lasr/long/a45D16UhKJ4xeSSO/result') .headers({ 'Authorization': 'Bearer a447bdDAUeKORvmt' // 这里的a447bdDAUeKORvmt就是接口1取得的access_token }) .end(function (res) { if (res.error) throw new Error(res.error); console.log(res.raw_body); }); ~~~