OAuth1 And Basic Authentication

I have a library that provides a HttpHeaderModel model - HttpHeaderService.cs with unit tests in HttpHeaderServiceTest.cs. These values are what can be used in the code that does the http request.

This can also be downloaded from Nuget as CarlPaton.Common

Basic Authentication

This is added to the Header of the request in the format name and value. The name is Authorization and the value is the word Basic and a base 64 encoded string of username:password. Example: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

1
2
3
4
var auth = string.Format("{0}:{1}", "username", "password");
var plainTextBytes = Encoding.ASCII.GetBytes(auth);
var base64Encoded = System.Convert.ToBase64String(plainTextBytes);
var completeHeader = string.Format("Basic {0}", base64Encoded);
1
2
3
4
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json; charset=utf-8";
request.Headers.Add("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")

OAuth1

OAuth1 is deprecated, please use OAuth 2.0

This is added to the Header of the request in the format name and value. The name is Authorization and the value is the word OAuth followed by concatinated string of key/values oauth_consumer_key, oauth_token, oauth_signature_method, oauth_timestamp, oauth_nonce, oauth_version and oauth_signature.

The oauth_signature is simply the consumerSecret & tokenSecret joined by & as url-encoding %26

Example:

1
$"OAuth oauth_consumer_key=\"{consumerKey}\",oauth_token=\"{accessToken}\",oauth_signature_method=\"PLAINTEXT\",oauth_timestamp=\"{timeStamp}\",oauth_nonce=\"{nonce}\",oauth_version=\"1.0\",oauth_signature=\"{consumerSecret}%26{tokenSecret}\""
1
2
3
4
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json; charset=utf-8";
request.Headers.Add("Authorization", "OAuth oauth_consumer_key=\"ConsumerKey\",oauth_token=\"AccessToken\",oauth_signature_method=\"PLAINTEXT\",oauth_timestamp=\"1579220112\",oauth_nonce=\"ShPKxisPFC0\",oauth_version=\"1.0\",oauth_signature=\"ConsumerSecret%26TokenSecret\"");

References