Vonage / vonage-dotnet-sdk

Vonage REST API client for .NET, written in C#. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
https://developer.vonage.com/
Apache License 2.0
106 stars 82 forks source link

ArgumentException: You must provide at least one type of authentication via configuration! #34

Closed VagyokC4 closed 7 years ago

VagyokC4 commented 7 years ago

I'm trying to verify the configuration using:

var nexmoSettings = Nexmo.Api.Configuration.Instance.Settings;

which throws the following error:

System.TypeInitializationException occurred
  HResult=0x80131534
  Message=The type initializer for 'Nexmo.Api.Configuration' threw an exception.
  Source=Nexmo.Api
  StackTrace:
   at Nexmo.Api.Configuration.get_Instance()
   at WEB.Startup..ctor(IHostingEnvironment env) in C:\Users\Dev\Source\Repos\nexmo-project\WEB\Startup.cs:line 59

Inner Exception 1:
ArgumentException: You must provide at least one type of authentication via configuration!

I have the following in my appsettings.json

{
  "Nexmo.UserAgent": "myApp/1.0",
  "Nexmo.Url.Rest": "https://rest.nexmo.com",
  "Nexmo.Url.Api": "https://api.nexmo.com",
  "Nexmo.api_key": "...",
  "Nexmo.api_secret": "..."
}

How can I configure this library through code? since it appears it's not picking up from the json file.

smithrobs commented 7 years ago

Unfortunately, depending on how you run the app it can be tricky to say exactly where the json needs to be. You can turn on logging to get a better idea of where the file is being looked for. Also, check out the quickstart example (there are two branches - one for aspnet and one for netcore).

Finally, we are switching from "automatic" config to instance config via code in 3.0. I haven't pushed out a package to NuGet yet, but will do so soon.

billbgarland commented 7 years ago

Hi, it seems like many people are having problems configuring the Nexmo library code in .NET. I am trying to set up the config in web.config which should work with v2.2.1 but I get the same exception.

I am new to .NET programming and struggling to understand why configuring the library is so difficult. It would be useful for me if Rob could explain the mechanism his code uses to get the config. In my application I use ConfigurationManager.AppSettings and this seems to work fine. Bill

billbgarland commented 7 years ago

I gave up and created a my appsettings.json file which sorted the problem. I am now struggling with JWT and how to configure Nexmo.Application.Key

smithrobs commented 7 years ago

appsettings.json works much better in general and is the official MS solution for going forward, so awesome! :)

Nexmo.Application.Key should be the path to your saved (on disk) application private key that you received when registering your application with Nexmo.

If you've joined the Nexmo Slack, it might be better to chat with someone there (possibly even me) to walk through any questions.

dpihac commented 7 years ago

I'm using LINQPad to do some quick prototyping and lack of code based configuration is a big problem for me. Disappointing.

NicoJuicy commented 7 years ago

Yeah, not a fan of the magic credentials either. Their should be a method where the credentials can be supplied. @smithrobs you are mentioning it yourselve, it can be tricky where the .config/.json files need to be located.

Setting up the credentials has taken 90% of the time for sending a personalised message, through a template message for multiple clients, where i fetch their data through odata on a cloud api.

This shouldn't be, i setup a dll with the code + a unit test and have the following files ( in both the dll's):

None seem to work. So please provide an alternative method for authentication.

Do i deploy them in the dll or in the unit test? What are the parths are checked. Can't this be supplied in the error message instead of "You must provide at least one type of authentication via configuration!" ? Also, for the json file, you have the following code: Process.GetCurrentProcess().MainModule.FileName here: https://github.com/Nexmo/nexmo-dotnet/blob/094c9e05c4f4504fba74f8fa8544d5330134bb65/Nexmo.Api/Configuration.cs

When debugging, it gives me: vstest.executionengine.x86.exe ... Which seems weird i have to create vstest.executionengine.x86.exe.config somewhere

EDIT!: I see SMS.Send has a credentials overload. So my problem is fixed, there is an alternative authentication :) - Good work ! ( off course, i noticed it later when forking :) )

@dpihac @billbgarland @VagyokC4 Will above fix your problem?

Edit 2: Nevermind, it doesn't work :(

smithrobs commented 7 years ago

@NicoJuicy I would never suggest for you to do not something you want to do, but I urge you to wait just until this weekend. I do have this mostly done in my 3.0 branch so your work would sadly be in vain.

In 3.0 the entire Nexmo.Api.Configuration is removed (see the branch) and everything is configurable via an instance.

Right, the credentials overload still unfortunately requires a valid config file and does not bypass that requirement.

smithrobs commented 7 years ago

@dpihac @billbgarland @VagyokC4 I'll be pushing to the 3.0 branch as well as a NuGet prerelease in just a little bit, but here's a quick preview of what you will be able to do:

var client = new Client(new Credentials
{
    ApiKey = "XXX",
    ApiSecret = "YYY"
});
var balance = client.Account.GetBalance();

I appreciate and welcome any feedback.

smithrobs commented 7 years ago

Also, just for housekeeping purposes I am closing this issue since it has turned into a discussion of #29. Please continue discussion there. Thanks!