none
I got this error using restsharp when trying to consume an API. RRS feed

  • Question

  • 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)"

    Wednesday, July 3, 2019 1:11 AM

All replies

  • Hello,

    I recommend posting here

    https://community.smartbear.com/t5/Swagger-Open-Source-Tools/bd-p/SwaggerOSTools


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 3, 2019 2:24 AM
  • I poster a question there but no one replies as of now. :(
    Wednesday, July 3, 2019 8:47 AM
  • I poster a question there but no one replies as of now. :(
    Sorry to hear that.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 3, 2019 9:20 AM
  • Swagger and Bearer Token is used by WebAPI posters in ASP.NET forums

    http://forums.asp.net/

    Wednesday, July 3, 2019 10:29 AM