简体   繁体   中英

Rust client for Twilio's API example program error

I am trying to get this exercise to run but keep getting a (permissions?) error on my AccountSid, which should be correct.

thread 'main' panicked at 'Something went wrong, ResponseError(ResponseContent { status: 401, content: "{\"code\": 20003, \"detail\": \"Your AccountSid or AuthToken was incorrect.\", \"message\": \"Authentication Error - invalid username\", \"more_info\": \"https://www.twilio.com/docs/errors/20003\", \"status\": 401}", entity: Some(UnknownValue(Object({"code": Number(20003), "detail": String("Your AccountSid or AuthToken was incorrect."), "message": String("Authentication Error - invalid username"), "more_info": String("https://www.twilio.com/docs/errors/20003"), "status": Number(401)}))) })'

Initially, I got a "this function takes 21 arguments but 18 arguments were supplied" error but fixed it by supplying the correct amount of None arguments to twilio_api::create_message

After that I get this AccoutSid error.

Here is the walk-through: https://www.twilio.com/docs/openapi/generating-a-rust-client-for-twilios-api

And here is my code for the exercise. I have the.env filled out correctly.

use dotenv;
use openapi::apis::{configuration::Configuration, default_api as twilio_api};
use std::env;

#[tokio::main]
async fn main() {
  // Securely import sensitive credentials and values from `.env` instead of inlining their values.
  dotenv::dotenv().expect("Failed to read .env file");
  let account_sid = env::var("TWILIO_ACCOUNT_SID").expect("Failed to parse Account SID");
  let api_key = env::var("TWILIO_API_KEY").expect("Failed to parse API Key");
  let api_key_secret = env::var("TWILIO_API_KEY_SECRET").expect("Failed to parse API Key Secret");
  let from = env::var("TWILIO_PHONE_NUMBER").expect("Failed to parse 'from' number");
  let to = env::var("TO_NUMBER").expect("Failed to parse 'to' number");

  // Create a new, default client configuration.
  let mut twilio_config = Configuration::default();
  // Apply your Basic Auth credentials to the client configuration.
  twilio_config.basic_auth = Some((api_key, Some(api_key_secret)));

  // Asynchronously send the message "Ahoy, Rustacean! 🦀" to the `to` number from your Twilio phone number.
  let message = twilio_api::create_message(
    &twilio_config,
    &account_sid,
    &to,
    None,
    None,
    None,
    Some("Ahoy, Rustacean! 🦀"),
    None,
    None,
    Some(&from),
    None,
    None,
    None,
    None,
    None,
    None,
    None,
    None,
    None,
    None,
    None,
  )
  .await;

  let result = match message {
    Ok(result) => result,
    Err(error) => panic!("Something went wrong, {:?}", error),
  };

  // Once successful, print out the SID of the sent message.
  println!("{:?}", result.sid);
}

Just got through the same error:

  • Install cargo-edit like the doc recommends, and use cargo add to download the crates. This resolved my error.

  • After that I had another issue with the parameters given in the example code, so I changed it like this:

  let body = "SMS test was successful!".to_string();

  let message = twilio_api::create_message(
    &twilio_config, // 1
    &account_sid,   // 2
    &to,            // 3
    None,           // 4
    None,  // 5
    None,           // 6
    None, // 7
    None,           // 8
    None,           // 9
    None,               // 10
    None,           // 11
    None,           // 12
    None,           // 13
    None,           // 14
    None,           // 15
    None,           // 16
    None,           // 17
    Some(&from),           // 18
    None,           // 19
    Some(&body),           // 20
    None,           // 21
  )
  .await;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM