Google Cloud Speech-To-Text とkintoneを連携させて文字起こしをしたい

kintoneの添付ファイル内の音声ファイル(MP3)をGoogle Cloud Speech-To-Textで文字起こしをしたいと考えています。

以下コードを書いたのですが、サーバーエラーとなりうまくいかず躓いている為アドバイス頂けませんでしょうか?

(function() {
  ‘use strict’;
 
// kintoneレコード追加画面で添付された音声ファイルの情報を取得
  kintone.events.on(‘app.record.edit.show’, function(event) {
    console.log(event)
    const fileKey = event.record[‘speech’].value[0].fileKey;
    const fileName = event.record[‘speech’].value[0].name;
    const fileType = event.record[‘speech’].value[0].contentType;
    // let blob;

    const apiKey = ‘××××××××’;
    const languageCode = ‘ja-JP’; // 認識する言語コード
    const url = kintone.api.urlForGet(‘/k/v1/file’, {fileKey: fileKey}, true);

    const promise1 = (url) => {
      return new Promise(resolve => {
        const xhr = new XMLHttpRequest();
        xhr.open(‘GET’, url);
        xhr.setRequestHeader(‘X-Requested-With’, ‘XMLHttpRequest’);
        xhr.responseType = ‘blob’;
        xhr.onload = () => {
          if (xhr.status === 200) {
        // ファイルがレスポンスとして返却されます。
          resolve(xhr.response);
          }
        };
        xhr.send();
      })
    };
  
    promise1(url).then(result1 => { 
      console.log(result1); 
      const fileReader = new FileReader();
      fileReader.readAsDataURL(result1);
      fileReader.onload = function() {
        const base64Data = fileReader.result.split(‘,’)[1];
        const requestData = {
          audio: { content: base64Data },
          config: { encoding: ‘LINEAR16’, languageCode: “ja-JP” }
        };
        console.log(base64Data);
      // APIにリクエストを送信して、認識結果を取得
    // Speech-To-Textを実行
    fetch(‘https://speech.googleapis.com/v1/speech:recognize?key=’ + apiKey, {
      method: ‘POST’,
      headers: {
        ‘Content-Type’: ‘application/json; charset=utf-8’
      },
      body: JSON.stringify(requestData)
    }).then((response) => {
      if(!response.ok) {
        console.error(‘サーバーエラー’);
      }
      // API実行結果を返却
      return response.text();
    }).then((text) => {
      // API実行結果をJSONパース
      let result_json = JSON.parse(text);
      // API実行結果から音声認識結果を取得
      text = result_json.results[0].alternatives[0].transcript;
      console.log(text);  
  
    }).catch((error)=>{
      console.error(‘通信に失敗しました’, error);
    });
      };
   });
  });
})();

 

      if(!response.ok) {
        console.error(‘サーバーエラー’);
      }

の、console.errorの行にブレークポイント張って止めて、responseの中身を見たり、ネットワークタブで何が返ってきているかなどを確認してみるのはいかがでしょうか?

※デベロッパーツールの使い方がわからない場合は、調べてください。

このトピックはベストアンサーに選ばれた返信から 3 日が経過したので自動的にクローズされました。新たに返信することはできません。