infobip / infobip-api-csharp-client

Infobip API client library in C#, distributed as a NuGet package.
https://www.infobip.com/docs/api
MIT License
11 stars 17 forks source link

StatusCode: Unauthorized, Content-Type: application/json;charset=UTF-8,Content-Length: 97) #1

Closed onadebi closed 6 years ago

onadebi commented 6 years ago

Hi, I'm trying to implement an API connection using C# as shown below. But its keeps throwing error of "StatusCode: Unauthorized, Content-Type: application/json;charset=UTF-8, Content-Length: 97)" Is there something I should be doing differently. please your urgent response is needed.

using RestSharp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace SmsWebService { public class OnaxInfoBip { public void OnxImplement() { try { var client = new RestClient("https://api.infobip.com/sms/1/text/single");

            string username = "OUsername";
            string password = "Opa$$word";
            byte[] concatenated = System.Text.ASCIIEncoding.ASCII.GetBytes(username + ":" + password);
            string header = String.Format("BASIC {0}",System.Convert.ToBase64String(concatenated));
            var request = new RestRequest(Method.POST);
            request.AddHeader("accept", "application/json");
            request.AddHeader("content-type", "application/json");
            request.AddHeader("authorization", header);
            request.AddParameter("application/json", "{\"from\":\"OSenderName\", \"to\":\"2348022897965\",\"text\":\"My Test MS.\"}", ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);
        }
        catch (Exception ex)
        {    throw;     }
    }
}

}

hkozacinski-ib commented 6 years ago

Hi,

as seen in the API documentation page for the method you are using, the thrown status code suggests the username and password provided are not authorized. My guess is the username and password you are using could have a typo or are not from a valid registered Infobip account. The account creation process is explained on the Introduction page. In case of further issues, feel free to contact us further.

Kind regards,

Hrvoje

onadebi commented 6 years ago

I have confirmed the login details several times. And even used it to log into InfoBip Dashboard. Nonetheless I still get the same error "StatusCode: Unauthorized, Content-Type: application/json;charset=UTF-8, Content-Length: 97)" when I try to send SMS through the API. What could be wrong?

Antolius commented 6 years ago

Hi onaefe!

By taking a look at the code snippet above, it looks to me like you are not using our library. If your intention was to use the lib, please find examples on how to do that in the examples directory. Specifically, this is how you can go about sending a single text sms: example.

As you can see from these examples, our library will handle base 64 encoding of your credentials and setup of http headers. All you'll have to deal with will be plain old C# objects representing the request and response. I'd personally advise you to take this approach.

On the other hand, you could, as you attempted to do, make http requests yourself. In that case, you are not using out library and are yourself responsible for formatting http headers, serialising request body to json format, etc.

In the example you gave above, you provided incorrect Authorization header. If using basic authorization your header should have a form of Basic base64(username:password) (you set it to BASIC ... and that does not work). To find out more about authorization methods please visit our official documentation.

Hope this helps you out, and looking forward to delivering your SMS messages soon! :)