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

Hi everyone!

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, 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

Answers

  • Hi @Leandro_LMCF , I see your having an error with your code. Those bugs can be so frustrating! Your best bet is to head over to the Twilio Collectives on Stack Overflow and ask your question there. There is a community of 1000 members that can help you!

    For your knowledge, Twilio Forums will be transitioning to Twilio Collectives and we will no longer be answering questions here. Please let me know if you have any questions or concerns about this transition.

If this is an emergency, please contact Twilio Support. This is not an official Support channel. https://support.twilio.com/
Have an urgent question?
Please contact Twilio Support. This is not an official Support channel.
Contact Support