Problem:
After a year of using C# code from the Shapeways recommended
.NET API Example no API calls worked. Using oauth parameters in the url not even the first simple access to
GET /oauth1/request_token/v1 worked anymore. It returned "authorization error".
After lots of testing I switched to the oauth variant storing parameters in the an HTTP header variable "Authorization" instead.
Have not yet figured out what had deteriorated in my code as it uses the underlaying oauth DLL from the sample and it is not rocket science.
Solution - Change to header based oauth:
To fix it, for anyone interested, in my client C# code I added a method that returns a HttpWebRequest instance that can take either form - url or header based. The sample contains an oauth call to
https://api.shapeways.com/models/v1 just to demonstrate how the HttpWebRequest is used :
Code:
//Oauth parameters storage alternatives in HTTP request
public enum par_location {header, url}
//Store oauth parameters in header as default
public par_location oauth_par_location = par_location.header;
//Return HttpWebRequest with either header or url based parameters
private HttpWebRequest createHttpWebRequest(OAuthRequest client)
{
HttpWebRequest request = null;
var url = client.RequestUrl;
switch (this.oauth_par_location)
{
case par_location.header:
request = (HttpWebRequest)WebRequest.Create(url);
request.Headers["Authorization"] = client.GetAuthorizationHeader();
break;
case par_location.url:
url += "?" + client.GetAuthorizationQuery();
request = (HttpWebRequest)WebRequest.Create(url);
break;
}
return request;
}
//Demo - Obtain model list
public object getModels() {
OAuthRequest client = new OAuthRequest() {
Method = "GET",
Type = OAuthRequestType.ProtectedResource,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
ConsumerKey = this.consumerKey,
ConsumerSecret = this.consumerSecret,
RequestUrl = "https://api.shapeways.com/models/v1",
Version = "1.0a",
Realm = "shapeways.com",
TokenSecret = this.OAuthSecret,
Token = this.OAuthToken,
};
HttpWebRequest request = this.createHttpWebRequest(client);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
return JsonConvert.DeserializeObject(content);
}
Great it works - Problem solved - But why did it stop working?
Once all my code was changed to initiate the HttpWebResponse with oauth params in the HTTP header all vent back to normal.
Strange..