Home > Mobile >  Error: Twilio.Exceptions.AuthenticationException: 'Username can not be null'
Error: Twilio.Exceptions.AuthenticationException: 'Username can not be null'

Time:04-13

I'm trying to create a Twilio app in C#, I took the code on the official documentation. I installed all the packages that the doc ask, for and try to run the app, but the following message error appears:

Code:

using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

TwilioClient.Init(
    Environment.GetEnvironmentVariable("********"),
    Environment.GetEnvironmentVariable("********")
);

var message = MessageResource.Create(
    from: new PhoneNumber("whatsapp:********"),
    to: new PhoneNumber("Whatsapp:********"),
    body: "Test"
);

Console.WriteLine("Message SID: ", message.Sid);

Error:

Unhandled exception. Twilio.Exceptions.AuthenticationException: Username can not be null
   at Twilio.TwilioClient.SetUsername(String username)
   at Twilio.TwilioClient.Init(String username, String password)
   at Program.<Main>$(String[] args) in 
line 5

I tried to implement the code line that appears in the error message, but the error is still there

Code:


using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

TwilioClient.Init(
    Environment.GetEnvironmentVariable("*********"),
    Environment.GetEnvironmentVariable("*********")
);

Twilio.TwilioClient.SetUsername(
    username: "*********@gmail.com"
);

Twilio.TwilioClient.Init(
    username: "*********@gmail.com", 
    password: "*********"
);

var message = MessageResource.Create(
    from: new PhoneNumber("whatsapp:*********"),
    to: new PhoneNumber("Whatsapp:*********"),
    body: "Teste"
);

Console.WriteLine("Message SID: ", message.Sid);

Something important to mention is that I create and set my account on Twilio, so everything is all right

This happens when I debug:

Debug

CodePudding user response:

Twilio tutorials and samples will usually use environment variables, but you have to make sure you set the environment variables first. This is to avoid hard coding sensitive information like Account SID and Auth Token, which could then be accidentally committed to source control.

You're currently passing null to the first parameter of TwilioClient.Init which is why the error occurs. The reason that you're passing null is that the environment variable you're trying to pull isn't configured yet.

If you're running your .NET project from the .NET CLI, before running the project, configure the environment variables like this:

If you're using a bash or bash-like shell (mac/linux/unix):

export TwilioAccountSid=[TWILIO_ACCOUNT_SID]
export TwilioAuthToken=[TWILIO_AUTH_TOKEN]

If you're using PowerShell:

export TwilioAccountSid=[TWILIO_ACCOUNT_SID]
export TwilioAuthToken=[TWILIO_AUTH_TOKEN]

If you're using CMD:

set "TwilioAccountSid=[TWILIO_ACCOUNT_SID]"
set "TwilioAuthToken=[TWILIO_AUTH_TOKEN]"

Once you close your shell, the environment variables are lost and you'll need to set them again next time.

If you're not running the project from the CLI, but from an IDE, the IDE may also let you configure environment variables, but depending on the IDE, it may be stored in a plain text file inside of your project, which introduces the same risk of accidentally putting it into source control and leaking the sensitive tokens.

You can also set environment on Windows, profile or system wide using their built-in UI.

Lastly, the samples use Environment Variables because they are universally available (on all OS's and infrastructure) and are the simplest way to safely get started, but .NET has a much better way of configuring applications. Here's an article that will take you from using environment variables to the .NET Configuration Builder and some of its features.

PS, let us know how you're running your project, like what IDE, what shell, etc. and also what Operating System you're using, so we can provide more specific instructions.

CodePudding user response:

Please try the following code snippet:

Set Twilio Account SID (String Identifiers at Twilio) as username twilio.com/docs/glossary/what-is-a-sid and Set Twilio Auth Token as a password

var sid = "34digitSID";
var authToken = "authToken";
TwilioClient.Init(sid, authToken);

For security reasons we can save this in appsettings.json file in asp.net MVC and read it as follows:

var sid = Configuration.GetValue<string>("AppSettings:Twilio:AccountSID");
var authToken = Configuration.GetValue<string>("AppSettings:Twilio:AuthToken");
TwilioClient.Init(sid, authToken);
        
  • Related