1

Credentials are right, because I can get an API response using PS with the same client id and secret. The token isn't invalid, but it won't get attached correctly to the rest request

Unauthorized. Access token is missing or invalid

Here's my code:

var client = new RestClient(url);
client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator("Bearer: " + OAuthToken);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Accept", "application/json");

foreach (var paramName in parameters.Keys) {
    request.AddParameter(paramName, parameters[paramName]);
}

request.RequestFormat = DataFormat.Json;

IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK) {
    string rawResponse = response.Content;
    dynamic deserializedResponse = new JsonDeserializer().Deserialize<dynamic>(response);
    return deserializedResponse;
}
else {
    Dictionary<string, string> returnData = new JsonDeserializer().Deserialize<Dictionary<string, string>>(response);
    throw new Exception("Failed call to API Management: " + string.Join(";", returnData));
 }

I've also tried using:

  1. request.AddHeader("authorization", "Bearer " + OAuthToken);
  2. request.AddHeader("authorization", string.Format("Bearer " + OAuthToken));
  3. request.AddHeader("authorization", string.Format("Bearer: " + OAuthToken));
  4. request.AddHeader("authorization", $"Bearer {OAuthToken}");
  5. request.AddParameter("authorization, "Bearer " + OAuthToken", HttpRequestHeader);
  6. request.AddHeader("authorization", "bearer:" + access + "");

None worked.

Kruti Shah
  • 11
  • 1
  • 1
  • 3

7 Answers7

3

Following code worked for me:

var restClient = new RestClient(Url)
{
    Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(accessToken, "Bearer")
};

As a result, the "Authorization" header will contain "Bearer {accessToken}"

  • 1
    Oh man on their website https://restsharp.dev/authenticators.html#access-token they provided a wrong example. That's why it didn't worked for me in the first place. Your example is correct. Thanks! – Rainer May 03 '22 at 08:53
2

I was not able to authenticate when I was using it like

request.AddHeader("Authorization", $"Bearer {axcessToken}");

instead this worked for me

client.AddDefaultHeader("Authorization", $"Bearer {axcessToken}");

0

You don't need the Authenticator. First, you should decorate the controller or the action like below:

[Authorize(AuthenticationSchemes = "Bearer")]
public class ApiServiceController : Controller
{
}

or better than that:

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ApiServiceController : Controller
{
}

Then you should add token bearer as this line:

request.AddParameter("Authorization", $"Bearer {OAuthToken}", ParameterType.HttpHeader);

where OAuthToken is the value of the received token from login.

If you need more codes, just tell me ;)

Hamid Baghernia
  • 565
  • 5
  • 14
0

Question is old but for any one coming to this again.. this is what worked for me: My project was configured to use Https, and I was not sending an Https request so server was sending me back a response informing that I should be using a Https request instead. After that, RestSharp performs automatically a redirect using Https this time, but is not including the Authorization Header. Mor infor here: https://github.com/restsharp/RestSharp/issues/414

My solutions was just to change my web api Url to use Https https://.../api/values

0

Not sure if this will help anyone, but in my case the problem was JWT issue time. I was using current time, and the server was a few seconds behind. I noticed that the JWT token was working when I was stepping through the code, but not when I was running it without pausing. I fixed the problem by subtracting 1 minute from JWT issue time.

Eternal21
  • 4,190
  • 2
  • 48
  • 63
0

Use

var client = new RestClient(URL);
client.AddDefaultHeader("Authorization", string.Format("Bearer {0}", accessToken));
Shammie
  • 131
  • 1
  • 7
0

I had the same issue in ASP.NET Framework. Using the AddParameter, as below, worked.

RestClient client = new RestClient(Url);
RestRequest request = new RestRequest(Method.POST);
request.AddParameter("token", _OsiApiToken);
request.AddParameter("value", value);
IRestResponse response = client.Execute(request);

Prior to the above (working version) I had the Url as...

String.Format("https://myorg.locator.com/arcgis/rest/services/something/?token={0}&amp;value={1}", X, Y)

Strangely the latter String.Format() worked in one project but not in another. Weird.

cymorg
  • 534
  • 2
  • 10
  • 27