0

The Node.JS code below sends 0-legged OAuth authenticated request to the API:

'use strict';
var OAuth = require('OAuth');
var express = require('express');
var app = express();

var oauth = new OAuth.OAuth(
  'http://example.com/oauth/request_token',
  'http://example.com/oauth/access_token',
  'mykey',
  'none',
  '1.0',
  null,
  'HMAC-SHA1'
);

app.get('/', function (req, res) {
  oauth.get(
    'http://example.com/api',
    'token123',
    'tokensecret123',
    function (error, data, response){
      data = JSON.parse(data);
      res.json(data);
    });
});

I need to convert this code to C# or VB.NET. Any sample of OAuth authenticated request in .Net will help too.

1
  • This is very simple in node because you have a library require('OAuth') that you're pulling in that makes it simple. Doing this in .NET is going to require some effort to find a library that will allow you to do the same thing, and probably with a lot more code. If there exists a .NET library that will let you do this in what is basically 10 lines of code, I want to know what it is!! Commented Jan 18, 2020 at 3:50

2 Answers 2

1

I do it with the library RestSharp which helps to deal with REST API.

The code below send a request to get a token from the OAuth:

var restClient = new RestClient();
restClient.BaseUrl = new Uri("theApiBaseUrl");

string encodedCredentials = Convert.ToBase64String(Encoding.Default.GetBytes($"yourAppId:yourSecret"));

// change the request below per the API requirement
RestRequest request = new RestRequest("theApiUrlForAuthentication", Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Authorization", $"Basic {encodedCredentials}");
request.AddQueryParameter("grant_type", "client_credentials");
request.AddQueryParameter("scope", "api");

IRestResponse response = restClient.Execute(request);

// the token should be in the JSON string response.Content
// now you'll want to deserialize the JSON to get the token
var jsonWithToken = MyFunctionToGetToken(response.Content);

Now you have the token in order to do authenticated calls to the API:

var restClient = new RestClient();
restClient.BaseUrl = new Uri("theApiBaseUrl");

RestRequest request = new RestRequest("theApiEndpoint", Method.GET);
request.AddHeader("Accept", "application/hal+json");
request.AddHeader("profile", "https://api.slimpay.net/alps/v1");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", $"Bearer {token}");

RestClient.Execute(request);

Each API is different, so you'll surely have to modify my code (add or remove headers, encoding the credentials, ...) so that it works for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you @Guillaume Sasdy for steering me towards RestSharp. Here is a working solution that works the same way as the node.js code in my question. Since API I'm accessing is using 0-legged OAuth, the Access Token and Access Secret are known upfront and make things much easier.

const string consumerKey = "mykey";
const string consumerSecret = "none";
var baseUrl = "https://example.com";
var client = new RestClient(baseUrl);

var request = new RestRequest("/api");
client.Authenticator = OAuth1Authenticator.ForProtectedResource(
    consumerKey, consumerSecret, "token123", "tokensecret123"
);

var response = client.Execute(request);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.