Lab setup instructions and requirements
Protect your account and progress. Always use a private browser window and lab credentials to run this lab.

Speech-to-Text API:Qwik Start

Lab 10 minutes universal_currency_alt 1 Credit show_chart Introductory
info This lab may incorporate AI tools to support your learning.
This content is not yet optimized for mobile devices.
For the best experience, please visit us on a desktop computer using a link sent by email.

GSP119

Google Cloud 自學實驗室

總覽

Speech-to-Text API 可將 Google 語音辨識技術輕鬆整合至開發人員應用程式,並將收到的音訊轉錄成文字。

課程內容

在本實驗室中,您將瞭解如何執行下列工作:

  • 建立 API 金鑰
  • 建立 Speech-to-Text API 要求。
  • 呼叫 Speech-to-Text API

設定和需求

瞭解以下事項後,再點選「Start Lab」按鈕

請詳閱以下操作說明。實驗室活動會計時,且中途無法暫停。點選「Start Lab」後就會開始計時,顯示可使用 Google Cloud 資源的時間。

您將在真正的雲端環境完成實作實驗室活動,而不是模擬或示範環境。為此,我們會提供新的暫時憑證,供您在實驗室活動期間登入及存取 Google Cloud。

為了順利完成這個實驗室,請先確認:

  • 可以使用標準的網際網路瀏覽器 (Chrome 瀏覽器為佳)。
注意事項:請使用無痕模式 (建議選項) 或私密瀏覽視窗執行此實驗室,這可以防止個人帳戶和學員帳戶之間的衝突,避免個人帳戶產生額外費用。
  • 是時候完成實驗室活動了!別忘了,活動一旦開始將無法暫停。
注意事項:務必使用實驗室專用的學員帳戶。如果使用其他 Google Cloud 帳戶,可能會產生額外費用。

如何開始研究室及登入 Google Cloud 控制台

  1. 點選「Start Lab」按鈕。如果實驗室會產生費用,畫面上會出現選擇付款方式的對話方塊。左側的「Lab Details」窗格會顯示下列項目:

    • 「Open Google Cloud console」按鈕
    • 剩餘時間
    • 必須在這個研究室中使用的臨時憑證
    • 完成這個實驗室所需的其他資訊 (如有)
  2. 點選「Open Google Cloud console」;如果使用 Chrome 瀏覽器,也能按一下滑鼠右鍵,選取「在無痕視窗中開啟連結」

    接著,實驗室會啟動相關資源,並開啟另一個分頁,顯示「登入」頁面。

    提示:您可以在不同的視窗中並排開啟分頁。

    注意:如果頁面中顯示「選擇帳戶」對話方塊,請點選「使用其他帳戶」
  3. 如有必要,請將下方的 Username 貼到「登入」對話方塊。

    {{{user_0.username | "Username"}}}

    您也可以在「Lab Details」窗格找到 Username。

  4. 點選「下一步」

  5. 複製下方的 Password,並貼到「歡迎使用」對話方塊。

    {{{user_0.password | "Password"}}}

    您也可以在「Lab Details」窗格找到 Password。

  6. 點選「下一步」

    重要事項:請務必使用實驗室提供的憑證,而非自己的 Google Cloud 帳戶憑證。 注意:如果使用自己的 Google Cloud 帳戶來進行這個實驗室,可能會產生額外費用。
  7. 按過後續的所有頁面:

    • 接受條款及細則。
    • 由於這是臨時帳戶,請勿新增救援選項或雙重驗證機制。
    • 請勿申請免費試用。

Google Cloud 控制台稍後會在這個分頁開啟。

注意:如要使用 Google Cloud 產品和服務,請點選「導覽選單」,或在「搜尋」欄位輸入服務或產品名稱。「導覽選單」圖示和搜尋欄位

工作 1:建立 API 金鑰

您將使用 curl 傳送要求至 Speech-to-Text API,因此必須產生 API 金鑰以傳送要求網址。

  1. 為建立 API 金鑰,請依序點選導覽選單 >「API 和服務」>「憑證」

  2. 點選「+ 建立憑證」

  3. 在下拉式選單,選取「API 金鑰」

  4. 在「建立 API 金鑰」側邊面板的「可透過這個金鑰存取的 API」下方,點選下拉式選單 (選取 API 限制)。

  5. 勾選「Cloud Speech-to-Text API」的方塊,然後點選「確定」

  6. 點選「建立」

  7. 複製剛才產生的金鑰,然後點選「關閉」

點選「Check my progress」,確認目標已達成。

建立 API 金鑰

您現在已擁有 API 金鑰,儲存為環境變數後,就不需要為每個要求插入 API 金鑰值。

為進行後續步驟,請使用 SSH 連線至為您佈建的執行個體。

  1. 依序點選導覽選單 >「Compute Engine」>「VM 執行個體」。「VM 執行個體」視窗中應該會顯示 linux-instance

  2. 點按 linux-instance 旁的「SSH」按鈕。系統會將您帶往互動式殼層。

  3. 在指令列中輸入下列內容,並將 <YOUR_API_KEY> 換成先前產生並複製的 API 金鑰:

export API_KEY=<YOUR_API_KEY>

實驗室接下來的操作,都會在這個 SSH 工作階段中執行。

工作 2:建立 Speech-to-Text API 要求

注意:您將使用預先錄好的檔案,該檔案可從 Cloud Storage 取得:gs://cloud-samples-tests/speech/brooklyn.flac請先聆聽音訊檔案,再傳送至 Speech-to-Text API
  1. 在 SSH 指令列中建立 request.json。您將使用下列指令建立要求並傳送至 Speech-to-Text API:
touch request.json
  1. 開啟 request.json
nano request.json 注意: 您可以使用偏好的指令列編輯器 (nanovimemacs) 或 gcloud。本實驗室將提供 nano 的操作說明。
  1. 使用原始音訊檔案範本的 uri 值,將下列指令新增至 request.json 檔案:
{ "config": { "encoding":"FLAC", "languageCode": "en-US" }, "audio": { "uri":"gs://cloud-samples-tests/speech/brooklyn.flac" } }
  1. 按下 control + x 再按 y 即可儲存檔案,接著按下 Enter 來關閉 request.json 檔案。

要求主體包含 configaudio 物件。

您可以在 config 指示 Speech-to-Text API 如何處理要求。encoding 參數說明檔案傳送至 API 時使用的音訊編碼類型,FLAC 則是 .raw 檔案的編碼類型。請參閱 RecognitionConfig 指南,進一步瞭解編碼類型。

您也可以新增其他參數至 config 物件,但 encoding 是唯一必填項目。

透過 audio 物件,您可以將 Cloud Storage 中音訊檔案的 URI 傳送至 API。

點選「Check my progress」,確認目標已達成。

建立 Speech-to-Text API 要求

現在,您可以呼叫 Speech-to-Text API 了!

工作 3:呼叫 Speech-to-Text API

  1. 使用下列 curl 指令,傳送要求主體及 API 金鑰環境變數至 Speech-to-Text API。請在單一指令列中輸入所有指令:
curl -s -X POST -H "Content-Type: application/json" --data-binary @request.json \ "https://speech.googleapis.com/v1/speech:recognize?key=${API_KEY}"

回應內容應如下所示:

{ "results": [ { "alternatives": [ { "transcript": "how old is the Brooklyn Bridge", "confidence": 0.9311077 } ], "resultEndTime": "1.770s", "languageCode": "en-us" } ], "totalBilledTime": "2s", "requestId": "1573191382345569448" }

transcript 值會傳回 Speech-to-Text API 將音訊檔案轉錄為文字的結果,confidence 值則表示 API 對準確轉錄音訊的信心程度。

您會注意到上方要求呼叫了 syncrecognize 方法。Speech-to-Text API 支援同步和非同步的語音轉文字作業。在這個例子中,您是將完整的音訊檔案傳送至 API,您也能使用 syncrecognize 方法,在使用者說話的同時將語音轉錄為文字。

您已建立 Speech-to-Text API 要求並呼叫 Speech-to-Text API。

  1. 執行下列指令,將回應內容儲存至 result.json 檔案:
curl -s -X POST -H "Content-Type: application/json" --data-binary @request.json \ "https://speech.googleapis.com/v1/speech:recognize?key=${API_KEY}" > result.json

點選「Check my progress」,確認目標已達成。

呼叫 Speech-to-Text API

恭喜!

您已使用 Speech-to-Text API 取得輸入音訊檔案的轉錄稿。

後續行動/瞭解詳情

本實驗室屬於 Qwik Start 實驗室系列,旨在帶您一窺 Google Cloud 的眾多功能。歡迎在實驗室目錄搜尋「Qwik Start」,看看接下來要參加哪個實驗室!

Google Cloud 教育訓練與認證

協助您瞭解如何充分運用 Google Cloud 的技術。我們的課程會介紹專業技能和最佳做法,讓您可以快速掌握要領並持續進修。我們提供從基本到進階等級的訓練課程,並有隨選、線上和虛擬課程等選項,方便您抽空參加。認證可協助您驗證及證明自己在 Google Cloud 技術方面的技能和專業知識。

使用手冊上次更新日期:2026 年 4 月 8 日

實驗室上次測試日期:2026 年 4 月 8 日

Copyright 2026 Google LLC 保留所有權利。Google 和 Google 標誌是 Google LLC 的商標,其他公司和產品名稱則有可能是其關聯公司的商標。

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

Use private browsing

  1. Copy the provided Username and Password for the lab
  2. Click Open console in private mode

Sign in to the Console

  1. Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
  2. Accept the terms, and skip the recovery resource page
  3. Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Using an Incognito or private browser window is the best way to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.