步驟 4:測試 API

本頁適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

如要測試已部署的 helloworld API,請在 Cloud Code 中的 Apigee 中按一下「Terminal」分頁標籤,然後輸入下列 curl 呼叫:

curl 0:8998/helloworld

系統會顯示下列錯誤訊息:

在終端機分頁中呼叫 API 並收到授權錯誤

因為您在建立 API Proxy 時已啟用 API 金鑰驗證機制,因此在呼叫 API 時,您必須使用 apikey 查詢參數傳遞 API 金鑰。

如要取得 API 金鑰,您必須建立測試套件,並設定下列測試資源:

  • API 產品:用於將 API 整合成套件,並提供給開發人員使用。

  • 開發人員:將建立應用程式來存取 API。

  • 開發人員應用程式:可使用 API 金鑰存取 API。

建構及匯出測試資源

  1. 建立測試套件:
    1. 將游標移至「test data」資料夾,然後按一下 建立測試套件圖示

      將游標移至測試資料夾時,畫面上會顯示 +

      「Create test bundle」精靈會隨即開啟。

    2. 輸入測試套件名稱「mytestbundle」,然後按下 Enter 鍵。

      測試套件已建立。

    3. 展開 mytestbundle 資料夾即可查看內容。

      包含 developerapps.json、developers.json、maps.json 和 products.json 檔案的測試資料夾

  2. 設定 API 產品測試資源:
    1. 將滑鼠游標移至 products.json 檔案,然後按一下 建立 API 產品圖示

      將游標置於 products.json 上時,系統會顯示 +

      系統會開啟「Create API product」精靈。

      「Create API product」精靈的第一頁

    2. 輸入 API 產品名稱「myproduct」,然後按下 Enter 鍵確認並繼續。
    3. 輸入 API 產品的說明「myproduct」,然後按下 Enter 鍵確認並繼續操作。
    4. 選取「helloworld」,將 API Proxy 新增至 API 產品,然後按一下「OK」

      系統會在編輯器中開啟 products.json 檔案,並設定新的 API 產品:

      [
        {
          "attributes": [
            {
              "name": "sample_attribute_0",
              "value": "sample_attribute_value_0"
            }
          ],
          "scopes": [],
          "environments": [],
          "apiResources": [
            "/",
            "/*",
            "/**"
          ],
          "quota": "100",
          "quotaInterval": "1",
          "quotaTimeUnit": "minute",
          "name": "myproduct",
          "displayName": "myproduct",
          "proxies": [
            "helloworld"
          ]
        }
      ]
  3. 設定開發人員測試資源:
    1. 將滑鼠游標移至 developers.json 檔案,然後按一下 建立 Apigee 開發人員圖示

      將游標置於 developers.json 上時,系統會顯示 +

      系統會開啟「Create developer」精靈。

      「Create API product」精靈的第一頁

    2. 在提示訊息中輸入下列值,然後按下 Enter 鍵確認並繼續操作:
      • 開發人員電子郵件地址:ahamilton@example.com
      • 使用者名稱:ahamilton
      • 名字:Alex
      • 姓氏:Hamilton

      系統會在編輯器中開啟 developers.json 檔案,並設定新的開發人員:

      [
          {
            "attributes": [
              {
                "name": "sample_attribute_0",
                "value": "sample_attribute_value_0"
              }
            ],
            "email": "ahamilton@example.com",
            "userName": "ahamilton",
            "firstName": "Alex",
            "lastName": "Hamilton"
          }
        ]
  4. 設定開發人員應用程式測試資源:
    1. 將滑鼠游標移至 developerapps.json 檔案,然後按一下 建立開發人員應用程式圖示

      將游標置於 developerapps.json 上時,系統會顯示 +

      「Create developer app」精靈會隨即開啟。

      「Create API product」精靈的第一頁

    2. 選取 ahamilton@example.com 做為應用程式的擁有者。
    3. 在下一個提示時輸入下列值,然後按下 Enter 鍵確認並繼續操作:
      • 應用程式名稱:myapp
      • 說明:myapp
      • 回呼網址:留空
      • 選取「myproduct」將其新增至應用程式,然後按一下「OK」
      • 選取「永不」做為到期日值。

      編輯器會開啟 developerapps.json 檔案,並設定新的應用程式:

      [
        {
          "attributes": [
            {
              "name": "sample_attribute_0",
              "value": "sample_attribute_value_0"
            }
          ],
          "developerEmail": "ahamilton@example.com",
          "name": "myapp",
          "displayName": "myapp",
          "callbackUrl": "",
          "apiProducts": [
            "myproduct"
          ],
          "expiryType": "never"
        }
      ]
  5. 將測試資源匯出至 Apigee Emulator。

    將滑鼠游標懸停在 mytestbundle 測試套件上,然後按一下 「匯出測試資源」圖示

    有效測試資源,包括 API 產品、開發人員和開發人員應用程式

    如以下圖所示,Apigee Emulators 區段會顯示有效的測試資源。

    有效測試資源,包括 API 產品、開發人員和開發人員應用程式

取得 API 金鑰

取得開發人員應用程式的 API 金鑰憑證:

  1. 在「模擬器」部分的「有效測試資料」下方,點選「有效開發人員應用程式」

    編輯器會開啟有效的開發人員應用程式設定檔。檔案包含 myproduct 的憑證,如下圖所示。

    開發人員應用程式,其中 consumerKey 值已醒目顯示。
  2. 複製 myproduct API 產品的 consumerKey 值 (不含引號)。

測試 API

使用 API 金鑰測試 API:

  1. 使用 apikey 查詢參數,在 curl 呼叫中傳遞 API 金鑰,如下所示:
    curl 0:8998/helloworld?apikey=API_KEY

    系統會傳回以下回應:

    Hello, Guest!

恭喜!您已成功在本機環境中執行 API。

在下一個步驟中,您將瞭解如何變更目標端點。

1 2 3 4 8