Twilio Auth windows 环境变量 [英] Twilio Auth windows enviro variables

查看:41
本文介绍了Twilio Auth windows 环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

创建此代码的大部分信息均来自

第二个:

  • '变量名'这样写:TWILIO_AUTH_TOKEN
  • 'variable value' 放置您的 twilio 身份验证令牌,如下所示:0123456789abcdefabcdefabcdefabcd

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

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

为此:

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

如果您打开了命令窗口,请将其关闭.您需要打开一个新的命令窗口以获取新的环境变量.

I'm putting together some python code to move and then delete recordings from Twilio. There's plenty of documentation online to help with the script but the documentation says that I need to add the authorization code and token as a windows variable. The documentation shows how to get to the correct location to add these variables but does not show exactly what to enter, which location to enter it into or the exact format that it needs. I'm am new to all of this. On my Windows 10 machine - in the new variable window - it asks for 'variable name' and 'variable value'. I need to know exactly what I put in and the format it should be in. Any help would be appreciated. Thanks!

Most information to create this code was gathered from 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'.

I don't have Windows 10 but I'm showing you on Widnows 7, it's probably the same interface to add system variables.

So you need to set two 'Environment Variables' > 'System Variables':

First one:

  • 'variable name' put this: TWILIO_ACCOUNT_SID
  • 'variable value' put your twilio account sid which looks something like this: AC0123456789abcdefabcdefabcdefabcd

Second one:

  • 'variable name' put this: TWILIO_AUTH_TOKEN
  • 'variable value' put your twilio auth token which looks something like this: 0123456789abcdefabcdefabcdefabcd

now back to your code change this:

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

to this:

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

If you had a command window open, close it. You need to open a new command window in order to pick up the new environment variables.

这篇关于Twilio Auth windows 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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