本頁適用於 Apigee 和 Apigee Hybrid。
查看
Apigee Edge 說明文件。
如要測試已部署的 helloworld
API,請在 Cloud Code 中的 Apigee 中按一下「Terminal」分頁標籤,然後輸入下列 curl 呼叫:
curl 0:8998/helloworld
系統會顯示下列錯誤訊息:
因為您在建立 API Proxy 時已啟用 API 金鑰驗證機制,因此在呼叫 API 時,您必須使用 apikey
查詢參數傳遞 API 金鑰。
如要取得 API 金鑰,您必須建立測試套件,並設定下列測試資源:
API 產品:用於將 API 整合成套件,並提供給開發人員使用。
開發人員:將建立應用程式來存取 API。
開發人員應用程式:可使用 API 金鑰存取 API。
建構及匯出測試資源
- 建立測試套件:
- 將游標移至「test data」資料夾,然後按一下
。
「Create test bundle」精靈會隨即開啟。
- 輸入測試套件名稱「mytestbundle」,然後按下 Enter 鍵。
測試套件已建立。
- 展開 mytestbundle 資料夾即可查看內容。
- 將游標移至「test data」資料夾,然後按一下
- 設定 API 產品測試資源:
- 將滑鼠游標移至 products.json 檔案,然後按一下
。
系統會開啟「Create API product」精靈。
- 輸入 API 產品名稱「myproduct」,然後按下 Enter 鍵確認並繼續。
- 輸入 API 產品的說明「myproduct」,然後按下 Enter 鍵確認並繼續操作。
- 選取「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" ] } ]
- 將滑鼠游標移至 products.json 檔案,然後按一下
- 設定開發人員測試資源:
- 將滑鼠游標移至 developers.json 檔案,然後按一下
。
系統會開啟「Create developer」精靈。
- 在提示訊息中輸入下列值,然後按下 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" } ]
- 將滑鼠游標移至 developers.json 檔案,然後按一下
- 設定開發人員應用程式測試資源:
- 將滑鼠游標移至 developerapps.json 檔案,然後按一下
。
「Create developer app」精靈會隨即開啟。
- 選取 ahamilton@example.com 做為應用程式的擁有者。
- 在下一個提示時輸入下列值,然後按下 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" } ]
- 將滑鼠游標移至 developerapps.json 檔案,然後按一下
- 將測試資源匯出至 Apigee Emulator。
將滑鼠游標懸停在 mytestbundle 測試套件上,然後按一下
。
如以下圖所示,Apigee Emulators 區段會顯示有效的測試資源。
取得 API 金鑰
取得開發人員應用程式的 API 金鑰憑證:
- 在「模擬器」部分的「有效測試資料」下方,點選「有效開發人員應用程式」。
編輯器會開啟有效的開發人員應用程式設定檔。檔案包含
myproduct
的憑證,如下圖所示。 - 複製
myproduct
API 產品的consumerKey
值 (不含引號)。
測試 API
使用 API 金鑰測試 API:
- 使用
apikey
查詢參數,在 curl 呼叫中傳遞 API 金鑰,如下所示:curl 0:8998/helloworld?apikey=API_KEY
系統會傳回以下回應:
Hello, Guest!
恭喜!您已成功在本機環境中執行 API。
在下一個步驟中,您將瞭解如何變更目標端點。