簡體   English   中英

您如何使用 IBM Cloud Function 中的 API 密鑰進行身份驗證?

[英]How do you authenticate with an API key inside an IBM Cloud Function?

我正在編寫一個 IBM Cloud Function,它使用 python SDK 與 Cloudant 服務交互。 我已啟動 Cloudant 服務,填充數據庫,並准備好服務憑證/API 密鑰。 但是,當我嘗試在我的 Function 中實例化 CloudantV1 服務時,出現運行時錯誤“必須提供身份驗證器”。

我在他們的 git 回購協議中查找了錯誤,它似乎試圖通過查找環境變量中的值來設置身份驗證器 object,這些值在 Function 中不存在。我只想直接傳遞我的 API 密鑰,但我有沒有找到執行此操作的方法。 我正在使用示例中的基本代碼,所以我認為我的調用是正確的。

我考慮過在 Function 中注入環境變量,但這聽起來像是一個大 hack。 我一定是做錯了什么。 請幫助我理解它是什么。 這是重現錯誤的基本 Function python 代碼:

from ibmcloudant.cloudant_v1 import CloudantV1
def main(params_dict):
    service = CloudantV1.new_instance()
    # unreachable
    return { "message" : "hello world" }

https://cloud.ibm.com/apidocs/cloudant?code=python#programmatic-authentication中有一個程序化身份驗證示例 - 它基本上如下所示:

from ibmcloudant.cloudant_v1 import CloudantV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator('yourAPIkey')
service = CloudantV1(authenticator=authenticator)
service.set_service_url('https://yourserviceurl.example')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM