Forum Discussion

Buchiman's avatar
Buchiman
Visitor
6 years ago

I got this error using restsharp when trying to consume an API in Swagger

I need to use an API from Swagger that sends email but I am having a problem in authorization I passed the token but still the result is Unauthorized.

I use the code below.

 

Dim url = "test123/test.com/" (can't include the actual)
    Dim apiKey = "12312"
    Dim apiPassword = "12312"
    Dim client = New RestClient(url)
    Dim request = New RestRequest("token", Method.POST)
    request.Parameters.Clear()
    request.AddParameter("grant_type", "password")
    request.AddParameter("username", apiKey)
    request.AddParameter("password", apiPassword)
    Dim response As IRestResponse = client.Execute(request)
    _pristrKey = response.Content
    Return JsonConvert.DeserializeObject(response.Content)

the content that i recieve is the Token from swagger but when i try to use it

Dim url = "test123/test.com/"
    Dim client = New RestClient(url)
    Dim request = New RestRequest("api/email", Method.POST)
    request.Parameters.Clear()
    request.AddHeader("accept", "application/json")
    request.AddParameter("Authorization", String.Format($"Bearer " + 
    _pristrKey), ParameterType.HttpHeader)
    request.AddHeader("Content-Type", "application/json")
    Dim apiInput = New With {Key .bulkId = "123", Key .from = 
    "test123@gmail.com", Key .subject = "Test", Key .text = "Test123", Key 
    .to = "test321@gmail.com"}
    request.AddParameter("application/json", 
    JsonConvert.SerializeObject(apiInput), ParameterType.RequestBody)
    request.RequestFormat = DataFormat.Json
    Dim response As IRestResponse = client.Execute(request)

I got this error in Header. {WWW-Authenticate=Bearer error="invalid_token"}

And recieve this:  response = "StatusCode: Unauthorized, Content-Type: , Content-Length: 0)"

No RepliesBe the first to reply