CURL调用导致ASR Web服务的意外响应

时间:2015-03-15 19:05:32

标签: c curl libcurl

以下错误是我尝试使用CURL将音频样本(从内存)上传到Nuance的ASR Web服务时获得的错误。我不确定我的代码中的错误在哪里,所以我希望有人可以帮我找出错误的位置。根据回复,错误是无效的应用程序ID或应用程序密钥,但我已直接从我的Nuance帐户粘贴该应用程序ID和密钥并多次检查。

代码应该执行以下操作:

  1. 将音频数据发布到Nuance的ASR网络服务
  2. 从ASR服务
  3. 获取识别结果(文本字符串)

    我的源代码如下所示:

    #include "curl.h"
    
    
    typedef struct {
        unsigned char* buffer;       
        unsigned int   size;         
        unsigned int   index;
    } transfer_state;
    
    struct curl_slist *slist = NULL;
    CURL* curl;
    int status = 0;
    char* audioDataPtr = NULL;
    transfer_state read_state, write_state;
    unsigned long sizeOfAudioInBytes;
    char recognitionResult[4096] = {0};
    
    
    
    static size_t read_data(char *bufptr, size_t size, size_t nitems, transfer_state *read_state) {
        size_t read;
    
        read = min(size * nitems, read_state->size - read_state->index);
        memcpy(bufptr, &(read_state->buffer[read_state->index]), read);
        read_state->index += read;
    
        return read / size;
    }
    
    static size_t write_data (void *ptr, size_t size, size_t nmemb, transfer_state *write_state) {
    
        memcpy(&(write_state->buffer[write_state->index]), ptr, size * nmemb);
        write_state->index += size * nmemb;
        //assert(write_state->index < write_state->size);
    
        return nmemb;
    }
    
    /* Function for loading audio */
    unsigned long loadAudioFile(char* audioFileName, char** audioData)
    {
       unsigned long sz = 0;
    
       FILE* fid = fopen(audioFileName, "r");
       fseek(fid, 0L, SEEK_END);
       sz = ftell(fid);
       fseek(fid, 0L, SEEK_SET);
       *audioData = (char*) malloc(sizeof(char) * sz);
       fread((void*) *audioData, 1, sz, fid);
       fclose(fid);
    
       return sz;
    }
    
    void main(void)
    {
       int n = 0;
       char* char_ptr = NULL;
       char contentLength[128] = {0};
    
       sizeOfAudioInBytes = loadAudioFile("audio_16k16bit.pcm", &audioDataPtr);
    
       read_state.buffer  = (unsigned char*) audioDataPtr;
        read_state.index   = 0;
        read_state.size    = sizeOfAudioInBytes;
    
        write_state.buffer = (unsigned char*) recognitionResult;
        write_state.index  = 0;
        write_state.size   = 4096;
    
       curl = curl_easy_init();
        if (curl == NULL)
       {
          printf("[%s:%d] ERROR. \n", __FUNCTION__, __LINE__);
       }
    
       status = curl_easy_setopt(curl, CURLOPT_URL,  "https://dictation.nuancemobility.net/NMDPAsrCmdServlet/dictation");
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
       status = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "appId=MYAPPID&appKey=MY128CHARAPPKEY"); 
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
       slist = curl_slist_append(NULL, "Content-Type:audio/x-wav;codec=pcm;bit=16;rate=16000");
       curl_slist_append(slist, "Content-Language:ENUS");
       curl_slist_append(slist, "Accept-Language:ENUS");
       curl_slist_append(slist, "Accept:text/plain");
       curl_slist_append(slist, "Accept-Topic:Dictation");
    
       memset(contentLength,0,128);
       sprintf(contentLength, "Content-Length:%d", sizeOfAudioInBytes);
       curl_slist_append(slist, contentLength);
    
    
       status = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
       status = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (char*) audioDataPtr);
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
       status = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) (sizeOfAudioInBytes) );
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
       status = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
       status = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&write_state);
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
       status = curl_easy_setopt(curl, CURLOPT_READDATA, (void*)&read_state);
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
       status = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_data);
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
       status = curl_easy_perform(curl);
       if (status != 0)
       {
          printf("[%s:%d] ERROR. Code: %d \n", __FUNCTION__, __LINE__, status);
       }
    
        n = write_state.index;
        recognitionResult[n] = 0; // possible problem if n == size!
    
       /* Locate first occurence of \n */
        char_ptr = strchr(recognitionResult, '\n');
        if (char_ptr != 0) 
       {
          /* Zero terminate string */
            *char_ptr = 0;
        }
    
       printf("%s\n", recognitionResult);
    
       free(audioDataPtr);
    
        curl_slist_free_all(slist);
    
       curl_easy_cleanup(curl);
    
    }
    

    这是我执行上述C代码时从服务器返回的内容:

      

    错误401未经授权,无效的应用程序ID   或密钥。

    HTTP错误401

    问题   访问/ NMDPAsrCmdServlet / dictation。原因是:

        Unauthorized,
    invalid application id or key.

    ..

    为了验证我的ASR帐户是否有效,我下载了一个curl可执行文件并执行了以下命令:

    curl“https://dictation.nuancemobility.net:443/NMDPAsrCmdServlet/dictation?appId=MYAPPID&appKey=MY128BYTEAPPKEY” - H“Content-Type:audio / x-wav; codec = pcm; bit = 16; rate = 16000”-H“Accept-Language:ENUS”-H“Content-长度:264522“-H”接受:application / xml“-H”Accept-Topic:Dictation“-k --data-binary @ audio_16k16bit.pcm -v

    并且工作正常:

    CURL response

1 个答案:

答案 0 :(得分:1)

我正在做一些非常相似的事情,我通过使用api密钥和通过邮件收到的ID解决了您所面临的问题,而不是http://dragonmobile.nuancemobiledeveloper.com

如果您可以帮助解决以下错误500:

Problem accessing /NMDPAsrCmdServlet/dictation. Reason:

x-nuance-sessionid*********************************

Received QueryRetry: 6 AUDIO_INFO

解决此问题后,当我使用codex发布音频文件时,我收到另一个错误500。不知道如何解决它。

谢谢。