-3

In POSTMAN Go to Authorization tab

  • Select OAuth 2.0 from the Type dropdown
  • Select Request Headers from Add authorization data to dropdown
  • Fill the following information in Configure New Token section:
  • Token Name: ‘My OAuth2 token’ (You can name it as your wish)
  • Grant Type: Client Credentials
  • Access Token URL: https://service.endpoint.com/api/oauth2/token
  • Client ID: The Client_Id generated earlier (ABCD)
  • Client Secret: The Client_Secret generated earlier(1234f)
  • Click on Get New Access Token button

I then need to make a get call using a bearer token.

I can get this to work in Postman, but have hit a wall trying to work out how to implement it in C#.

I need C# code for calling a REST API with an access token.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

-1

Here is a sample C# request to make and Oauth call with authorization token. Install the restSharp nuget package.

//#Install RestSharp nuget package
//using RestSharp
public async Task<RestResponse> getData(string authtoken)
{
    var options = new RestClientOptions("http://sampleUrl")
    {
        MaxTimeout = -1,
    };
    var client = new RestClient(options);
    var request = new RestRequest("/samplemethods/123", Method.Get);
    request.AddHeader("Authorization", "Bearer " + authtoken);
    RestResponse response = await client.ExecuteAsync(request);
    Console.WriteLine(response.StatusCode);
    return response;
}
sudhansu63
  • 6,025
  • 4
  • 39
  • 52