python - Twilio Auth Windows 环境变量

标签 python windows twilio twilio-api

我正在整理一些 python 代码以从 Twilio 移动和删除录音。有很多在线文档可以帮助脚本,但文档说我需要将授权代码和 token 添加为 Windows 变量。该文档显示了如何到达正确的位置以添加这些变量,但并未准确显示要输入的内容、将其输入的位置或所需的确切格式。我对这一切都是陌生的。在我的 Windows 10 机器上 - 在新变量窗口中 - 它要求输入“变量名称”和“变量值”。我需要确切地知道我输入的内容以及它应该采用的格式。任何帮助将不胜感激。谢谢!

创建此代码的大部分信息是从 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. e.g. 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!")

最佳答案

On my Windows 10 machine - in the new variable window - it asks for 'variable name' and 'variable value'.

我没有 Windows 10,但我在 Widnows 7 上向您展示,它可能是添加系统变量的相同界面。

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

第一个:

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

enter image description here

第二个:

  • '变量名'把这个:TWILIO_AUTH_TOKEN
  • “变量值”放置您的 twilio 身份验证 token ,它看起来像这样:0123456789abcdefabcdefabcdefabcd

enter image description here

现在回到您的代码更改此:

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

为此:

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

如果您打开了命令窗口,请将其关闭。您需要打开一个新的命令窗 Eloquent 能选择新的环境变量。

关于python - Twilio Auth Windows 环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37750822/

相关文章:

c++ - 使用 MinGW 检查 Boost 安装

Python Pexpect 和 Check Point Gaia 专家模式

python - bbfreeze 和 protobuf

python - 相似类型对象之间的类型转换设计

python - 如何在 gitlab-ci.yml 中将 postgis 扩展添加到 postgresql 数据库

javascript - 类型错误 : Cannot read property 'Digits' of undefined in twilio when gathering user input from call

.net - MS Access 驱动程序问题

windows - native x64 dll 不工作

php - 使用 PHP 通过 Twilio 发送短信

python - 如何在没有 twilio 的情况下使用 python 和 opencv 流式传输视频?