Twilio-需要“发件人”电话(aiohttp)

时间:2018-07-05 16:51:35

标签: twilio

我一遍又一遍地尝试使它起作用,但是它总是以

返回

{'code': 21603, 'message': "A 'From' phone number is required.", 'more_info': 'https://www.twilio.com/docs/errors/21603', 'status': 400}

有人可以帮忙吗?

我的代码:

    import asyncio, aiohttp

    async def main():
        auth = aiohttp.BasicAuth(login="[sid]", password="[token]")
        async with aiohttp.ClientSession(auth=auth) as session:
            async with session.post("https://api.twilio.com/2010-04-01/Accounts/[sid]/Messages.json", params={"Body": "Test", "From": "[valid phone number]", "To": "[valid phone number]"}) as resp:
                print(await resp.json())

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

1 个答案:

答案 0 :(得分:0)

使用data=payload,将代码更改为以下形式:


import asyncio, aiohttp

async def main():
    payload = {"Body": "Test", "From": "[valid phone number]", "To": "[valid phone number]"}
    auth = aiohttp.BasicAuth(login="[sid]", password="[token]")
    async with aiohttp.ClientSession(auth=auth) as session:
        async with session.post("https://api.twilio.com/2010-04-01/Accounts/[sid]/Messages.json", data=payload) as resp:
            print(await resp.json())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())