簡體   English   中英

Twilio 的 API 示例程序錯誤的 Rust 客戶端

[英]Rust client for Twilio's API example program error

我正在嘗試運行此練習,但在我的 AccountSid 上不斷收到(權限?)錯誤,這應該是正確的。

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

之后我得到這個 AccoutSid 錯誤。

這是演練:https://www.twilio.com/docs/openapi/generating-a-rust-client-for-twilios-api

這是我的練習代碼。 我正確填寫了.env。

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);
}

剛剛遇到同樣的錯誤:

  • 按照文檔推薦的方式安裝 cargo-edit,並使用 cargo add 下載 crates。 這解決了我的錯誤。

  • 之后我對示例代碼中給出的參數有另一個問題,所以我改變了它:

  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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM