创建 TwilioClient django 需要 TWILIO API 错误凭据 [英] TWILIO API ERROR Credentials are required to create a TwilioClient django

查看:44
本文介绍了创建 TwilioClient django 需要 TWILIO API 错误凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 TWILIO API 包含到我的项目中.它应该发送短信.我已经完成了教程,但随后出现错误创建 TwilioClient 需要凭据.我在 .env 文件中有凭据,然后我尝试将它们导入设置,然后从设置到视图获取此凭据.

I am trying to include TWILIO API to my project. It should send sms. I have finished tutorial, but then i get error Credentials are required to create a TwilioClient. I have credentials in .env file and then i try to import them to settings and then get this credentials from settings to views.

这是我出错的时候.

.env

TWILIO_ACCOUNT_SID= 'xxxxxxxxxxxxxxxxxxxxxx'
TWILIO_AUTH_TOKEN= 'xxxxxxxxxxxxxxxxxxxxxxx'
TWILIO_NUMBER= 'xxxxxxxxxxxxxxxxxx'

settings.py

settings.py

import os

    TWILIO_ACCOUNT_SID = os.getenv('TWILIO_ACCOUNT_SID')
    TWILIO_AUTH_TOKEN = os.getenv('TWILIO_AUTH_TOKEN')
    TWILIO_NUMBER = os.getenv('TWILIO_NUMBER')
    SMS_BROADCAST_TO_NUMBERS = [
        '+111111111', 
    ]   

观看次数

from django.conf import settings                                                                                                                                                       
from django.http import HttpResponse
from twilio.rest import Client


def broadcast_sms(request):
    message_to_broadcast = ("Have you played the incredible TwilioQuest "
                                                "yet? Grab it here: https://www.twilio.com/quest")
    client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
    for recipient in settings.SMS_BROADCAST_TO_NUMBERS:
        if recipient:
            client.messages.create(to=recipient,
                                   from_=settings.TWILIO_NUMBER,
                                   body=message_to_broadcast)
    return HttpResponse("messages sent!", 200)

这是代码工作的时间,但我想从设置中导入它..

and here is when code work, but i want to import this from settings..

# def sms(request):
#     TWILIO_ACCOUNT_SID = "xxxxxxxxxxxxxxxxxxxxxxx"
#     TWILIO_AUTH_TOKEN = "xxxxxxxxxxxxxxxxx"
#     TWILIO_NUMBER = "xxxxxxxxxxxxx"
#     message_to_broadcast = ("Have you played the incredible TwilioQuest "
#                                                 "yet? Grab it here: https://www.twilio.com/quest")
#
#     client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
#     for recipient in settings.SMS_BROADCAST_TO_NUMBERS:
#         if recipient:
#             client.messages.create(to=+xxxxxxxxx,
#                                    from_=+xxxxxxxxxx,
#                                    body=message_to_broadcast)
#     return HttpResponse("messages sent!", 200)

知道如何解决这个问题吗?

Any idea how to solve this?

推荐答案

所以您使用的是 .env 文件而不是设置操作系统的环境变量?如果是这样,下面有文章,指向 https://github.com/theskumar/python-dotenv.

So you are using a .env file rather than setting your OS's environmental variables? If so, there is and article below, pointing to https://github.com/theskumar/python-dotenv.

如何设置环境变量

这篇关于创建 TwilioClient django 需要 TWILIO API 错误凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆