Skip to content

Instantly share code, notes, and snippets.

@DavidStrickland0
Created December 22, 2015 15:58
Show Gist options
  • Save DavidStrickland0/888677646e64956e6871 to your computer and use it in GitHub Desktop.
Save DavidStrickland0/888677646e64956e6871 to your computer and use it in GitHub Desktop.
Upwork(formerly Odesk) API Call using C#
public static PublicInfoResponse LoadResponse()
{
PublicInfoResponse publicInfoResponse = (PublicInfoResponse)HttpRuntime.Cache["Odesk:PublicInfoResponse"];
if (publicInfoResponse == null)
{
Models.OAuthParams parameters;
string method = "GET";
parameters = new Models.OAuthParams();
parameters.Token = ConfigurationManager.AppSettings["apiKeys:UpworkoAuthToken"];
parameters.TokenSecret = ConfigurationManager.AppSettings["apiKeys:UpworkoAuthValue"];
parameters.ConsumerKey = ConfigurationManager.AppSettings["apiKeys:UpworkApiKey"];
parameters.ConsumerSecret = ConfigurationManager.AppSettings["apiKeys:UpworkApiSecret"];
var oDeskProfileKey = ConfigurationManager.AppSettings["apiKeys:UpworkProfileKey"];
string resource_url = string.Format("https://www.odesk.com/api/profiles/v1/providers/~{0}.json", oDeskProfileKey);
OAuthManager.GenerateOAuthParameters(parameters, resource_url, method);
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", parameters.Header);
request.Method = method;
request.ContentType = "application/x-www-form-urlencoded";
WebResponse response = request.GetResponse();
var responseStream = response.GetResponseStream();
var streamReader = new StreamReader(responseStream);
var body = streamReader.ReadToEnd();
JavaScriptSerializer ser = new JavaScriptSerializer();
publicInfoResponse = ser.Deserialize<PublicInfoResponse>(body);
HttpRuntime.Cache.Add("Odesk:PublicInfoResponse", publicInfoResponse, null, Cache.NoAbsoluteExpiration, new TimeSpan(1, 0, 0), CacheItemPriority.Normal, null);
}
return publicInfoResponse;
}
public static void GenerateOAuthParameters(Models.OAuthParams parameters, string resource_url, string method)
{
parameters.Version = "1.0";
parameters.SignatureMethod = "HMAC-SHA1";
parameters.Nonce = Convert.ToBase64String(
new ASCIIEncoding().GetBytes(
DateTime.Now.Ticks.ToString()));
var timeSpan = DateTime.UtcNow
- new DateTime(1970, 1, 1, 0, 0, 0, 0,
DateTimeKind.Utc);
parameters.Timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
//generate an encrypted oAuth signature which Twitter will use to validate the request. To do this, all of the request data is concatenated into a particular format as follows.
var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}";
var baseString = string.Format(baseFormat,
parameters.ConsumerKey,
parameters.Nonce,
parameters.SignatureMethod,
parameters.Timestamp,
parameters.Token,
parameters.Version
);
baseString = string.Concat(method, "&", Uri.EscapeDataString(resource_url),
"&", Uri.EscapeDataString(baseString));
//encrypt the data using a composite of the secret keys and the HMAC-SHA1 algorithm.
var compositeKey = string.Concat(Uri.EscapeDataString(parameters.ConsumerSecret),
"&", Uri.EscapeDataString(parameters.TokenSecret));
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
parameters.Signature = Convert.ToBase64String(
hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}
//generate the Authentication header. This requires concatenating the public keys and the token generated above into the following format.
var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
"oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
"oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
"oauth_version=\"{6}\"";
parameters.Header = string.Format(headerFormat,
Uri.EscapeDataString(parameters.Nonce),
Uri.EscapeDataString(parameters.SignatureMethod),
Uri.EscapeDataString(parameters.Timestamp),
Uri.EscapeDataString(parameters.ConsumerKey),
Uri.EscapeDataString(parameters.Token),
Uri.EscapeDataString(parameters.Signature),
Uri.EscapeDataString(parameters.Version));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment