Twilio Auth windows envirovariables

我正在编写一些Python代码来移动,然后从Twilio删除录音。 有大量的文档在线帮助脚本,但文档说,我需要添加授权代码和令牌作为一个Windowsvariables。 该文档显示了如何到达正确的位置以添加这些variables,但并未准确显示要input的内容,input的位置或所需的确切格式。 我对这一切都很陌生。 在我的Windows 10机器上 – 在新的variables口中 – 它要求“variables名称”和“variables值”。 我需要确切地知道我的内容和格式应该是在任何帮助,将不胜感激。 谢谢!

大部分的信息来自https://www.twilio.com/blog/2016/05/bulk-delete-your-twilio-recordings-with-python.html

from twilio.rest import TwilioRestClient import csv import threading from queue import Queue from datetime import date import os import requests from requests.auth import HTTPBasicAuth # Ensure your environmental variables have these configured account_sid = "{{ myaccountSID }}" auth_token = "{{ myToken }}" # Initialize Twilio Client client = TwilioRestClient(account_sid, auth_token) # Create a lock to serialize console output lock = threading.Lock() # The work method includes a print statement to indicate progress def do_work(recording_sid): client.recordings.delete(recording_sid) # Make sure the whole print completes or # threads can mix up output in one line. with lock: print(threading.current_thread().name, "has deleted", recording_sid) def do_work(recording): data = requests.get(recording.uri, auth=HTTPBasicAuth(), stream=True) # Create a .wav file and stream the recording to improve performance. with open(recording.sid + '.wav', 'wb') as fd: for chunk in data.iter_content(1): fd.write(chunk) client.recordings.delete(recording.sid) # Make sure the whole print completes or threads # can mix up output in one line. with lock: print(threading.current_thread().name, "has downloaded to the local folder and " "has been deleted off Twilio", recording_sid) que.task_done() # Create the queue and thread pool. # The range value controls the number of threads you run. que = Queue() for idx in range(20): thread = threading.Thread(target=worker) # thread dies when main thread (only non-daemon thread) exits. thread.daemon = True thread.start() # Open up a CSV file to dump the results of deleted recordings into with open('recordings.csv', 'w') as csvfile: record_writer = csv.writer(csvfile, delimiter=',') # Let's create the header row record_writer.writerow(["Recording SID", "Duration", "Date", "Call SID"]) # You can use a date filter if needed. eg before=date(2016, 5, 30) for recording in client.recordings.iter(before=date(2016, 5, 30)): record_writer.writerow([recording.sid, recording.duration, recording.date_updated, recording.call_sid]) que.put(recording) que.join() # block until all tasks are done print("All done!") 

Solutions Collecting From Web of "Twilio Auth windows envirovariables"

在我的Windows 10机器上 – 在新的变量窗口中 – 它要求“变量名称”和“变量值”。

我没有Windows 10,但我在Widnows 7上展示给你,它可能是添加系统变量的相同接口。

所以你需要设置两个“环境变量”>“系统变量”:

第一:

  • '变量名'把这个: TWILIO_ACCOUNT_SID
  • '变量值'把你的twilio账户sid看起来像这样: AC0123456789abcdefabcdefabcdefabcd

在这里输入图像说明

第二个:

  • '变量名'把这个: TWILIO_AUTH_TOKEN
  • '变量值'把你的twilio auth令牌看起来像这样: 0123456789abcdefabcdefabcdefabcd

在这里输入图像说明

现在回到你的代码改变这个:

 account_sid = "{{ myaccountSID }}" auth_token = "{{ myToken }}" 

对此:

 account_sid = os.environ.get('TWILIO_ACCOUNT_SID') auth_token = os.environ.get('TWILIO_AUTH_TOKEN') 

如果你打开了一个命令窗口,关闭它。 您需要打开一个新的命令窗口才能获取新的环境变量。

Twilio福音传教士在这里。

默认情况下, Twilio Python帮助程序库中的TwilioRestClient对象在当前环境中查找名为TWILIO_ACCOUNT_SIDTWILIO_AUTH_TOKEN环境变量。 以下是一篇文章,介绍如何在Windows上设置环境变量:

https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them

因此,要为您的帐户Sid设置环境变量,您将打开命令提示符并输入:

 c:\>set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXX 

在环境变量中保留凭据是保证这些凭据安全的好方法,并有助于防止像您不小心将它们检入到源代码管理中的情况。

如果您不想使用环境变量,则还可以通过构造函数直接向库提供凭据:

 from twilio.rest import TwilioRestClient ACCOUNT_SID = "AXXXXXXXXXXXXXXXXX" AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY" client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) 

希望有所帮助。