HttpClient unauthorized (401)

frutuopa

Member
Joined
Apr 4, 2022
Messages
8
Programming Experience
10+
Hello

I use this code to send SMS messages.
I have to obtain a token beforehand, which I do without any problems... When I try to send the message I always get an authorization error...
I simulated it with Postman and it works fine.

Can you help?

Thanks
VB.NET:
Private Async Sub btnEnviaSMS_Click(sender As Object, e As EventArgs) Handles btnEnviaSMS.Click

        Dim lsql As String = ""
        Dim aspas As String = Chr(34)
        Dim ghttpClient As New HttpClient
        Dim stoken As String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ1cm46XC9cL21wcm8tbTJtLWF1dGhvcml6YXRpb24iLCJleHAiOjE3Mzc0NTE1NjYsImlhdCI6MTczNzQ0Nzk2NiwiY2xpZW50X2lkIjoiODQzNjgyZDMtMmExMy00NzU2LThiYjEtOTFjNjI3YjcxY2FiIiwianRpIjoiMDhkNGNlMGYtOGUyMC00NjU4LWI2MDYtZDcxZmJjODdlZDc2In0.d5Zvgnn_noIbtFIzJ_VNtR6AuxEYNP0kcVHJQkoaR9GpscMUI3DmB7_PDC0c8ene2ssg7Uzmjlx8WRLw6slRQQ"
        Dim srecipient = <json>
            [{"to":"+351967605175"},{"to":"+351967605174"}]
        </json>

        Dim requestContent As New StringContent(JsonConvert.SerializeObject(New With {
                .recipients = srecipient,
                .sender = "Funfrap",
                .body = "OLA",
                .title = "TITULO",
                .dlr = "NONE",
                .ttl = "FOUR_HOURS",
                .smartEncode = "True",
                .smartText = "True"
            }), Encoding.UTF8, "application/json")

        Dim requestMessage As New HttpRequestMessage(HttpMethod.Post, "https://api-mpro.apg.nos.pt/communication/v1/sms") With {
            .Content = requestContent
        }
        requestMessage.Headers.Add("Authorization", stoken)
        requestMessage.Headers.Add("X-ApiKey", "843682d3-2a13-4756-8bb1-91c627b71cab")

        Dim response As HttpResponseMessage = Await ghttpClient.SendAsync(requestMessage)


        Dim responseString = response.Content.ReadAsStringAsync()
    End Sub

ERROR :


?response
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: keep-alive
Date: Tue, 21 Jan 2025 08:27:17 GMT
Content-Length: 151
Content-Type: application/json
}}
Content: {System.Net.Http.StreamContent}
Headers: {Connection: keep-alive
Date: Tue, 21 Jan 2025 08:27:17 GMT
}
IsSuccessStatusCode: False
ReasonPhrase: "Unauthorized"
RequestMessage: {Method: POST, RequestUri: 'https://api-mpro.apg.nos.pt/communication/v1/sms', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ1cm46XC9cL21wcm8tbTJtLWF1dGhvcml6YXRpb24iLCJleHAiOjE3Mzc0NTE1NjYsImlhdCI6MTczNzQ0Nzk2NiwiY2xpZW50X2lkIjoiODQzNjgyZDMtMmExMy00NzU2LThiYjEtOTFjNjI3YjcxY2FiIiwianRpIjoiMDhkNGNlMGYtOGUyMC00NjU4LWI2MDYtZDcxZmJjODdlZDc2In0.d5Zvgnn_noIbtFIzJ_VNtR6AuxEYNP0kcVHJQkoaR9GpscMUI3DmB7_PDC0c8ene2ssg7Uzmjlx8WRLw6slRQQ
X-ApiKey: 843682d3-2a13-4756-8bb1-91c627b71cab
Content-Type: application/json; charset=utf-8
Content-Length: 226
}}
StatusCode: Unauthorized {401}
Version: {1.1}
 
Last edited by a moderator:
Back
Top