0

我正在尝试使用基于云的营销通信软件套件Sendinblue.com的 SMTP API 发送电子邮件活动邮件。我正在运行 Sendinblue 提供的模板脚本的配置,如屏幕截图所示: Sendinblue 错误:找不到密钥

我的初始脚本如下:

# ------------------
# Create a campaign\
# ------------------
# Include the Sendinblue library\
from __future__ import print_function
import time
from datetime import datetime
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint
myapikey = 'xkeysib-.................................' # full api key
# Instantiate the client\
sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
# Define the campaign settings\
email_campaigns = sib_api_v3_sdk.CreateEmailCampaign(
name= "Campaign sent via the API",
subject= "My subject",
sender= { "name": "From name", "email": "------@gmail.com"}, # personal email with which I was registered @ sendinblue
type= "classic",
# Content that will be sent\
html_content= "Congratulations! You successfully sent this example campaign via the Sendinblue API.",
# Select the recipients\
recipients= {"listIds": [2, 7]},
# Schedule the sending in one hour\
scheduled_at = datetime.now()
)
# Make the call to the client\
try:
    api_response = api_instance.create_email_campaign(email_campaigns)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling EmailCampaignsApi->create_email_campaign: %s\n" % e)
    

在我有疑问的情况下,我参考了Sendinblue 文档。大部分过程似乎是不言自明的,只是与 的行configuration.api_key['api-key'] = 'YOUR API KEY' 是模棱两可的,因为它没有很清楚地说明应该如何传递 api-key 以及api_key的属性我假设api_key持有API 中定义的名称SMTP 和 API 高级选项卡。即使我假设它应该包含其他一些值,但也不清楚它们应该是什么,我可以肯定的是 sib_api_v3_sdk.configuration.api_key['api-key'] 也导致了AttributeError: module 'sib_api_v3_sdk.configuration' has no attribute 'api_key'.

在最初收到错误后,AttributeError: module 'sib_api_v3_sdk.configuration' has no attribute 'api_key'我研究了十几篇关于 StackOverflow 的文章,其中一篇是关于 Kubernetes 主题的,其中脚本似乎抛出了与我得到的错误类似的错误,所以我遵循了Python kubernetes 模块中描述的建议没有属性'api_key'。因此,我尝试重新分配类属性,如下所示:

configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()

现在我没有收到丢失密钥的错误,而是面临“未经授权”的消息,这一事实让我在短时间内振作起来,直到我花了几个小时试图克服这个问题。所以现在抛出的脚本现在HTTP response body: {"message":"Key not found","code":"unauthorized"}是这样的:

# ------------------
# Create a campaign\
# ------------------
# Include the Sendinblue library\
from __future__ import print_function
import time
from datetime import datetime
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint
myapikey = 'xkeysib-c..............' # key
# Instantiate the client\
sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
# Define the campaign settings\
email_campaigns = sib_api_v3_sdk.CreateEmailCampaign(
name= "Campaign sent via the API",
subject= "My subject",
sender= { "name": "From name", "email": "mail....@gmail.com"}, # personal gmail with which I was initially registered @ sendinblue.com
type= "classic",
# Content that will be sent\
html_content= "Congratulations! You successfully sent this example campaign via the Sendinblue API.",
# Select the recipients\
recipients= {"listIds": [2, 7]},
# Schedule the sending in one hour\
scheduled_at = datetime.now()      
)
# Make the call to the client\
try:
    api_response = api_instance.create_email_campaign(email_campaigns)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling EmailCampaignsApi->create_email_campaign: %s\n" % e)
    

我的第一个问题是:我的第一个脚本错了吗?其次:configuration.api_key['api-sb'] = myapikey假设 'api-sb' 是我的 API 密钥的名称,如屏幕截图所示,语法是否正确?第三:在myapikey变量中,在分配 API 密钥本身时,是否应该有其他任何东西作为密钥的前缀?

4

0 回答 0