Api with C# throwing 500 Internal Server Error while trying to get the Access_token
I am trying to implement 500px API with C#. I am able to authenticate user with 500px API but I am unable to get the access_token in exchange of response_token which leaves my third step of Oauth 1.0 incomplete. I am able to authorize user and get oauth_token and oauth_verifier but when I use this oauth_token for making following request
https://api.500px.com/v1/oauth/access_token
500 Internal Server Error along with the following screen gets thrown

I have debugged my code like thousand times, tried different ways to form URL, added various parameters to the request but no help. I am very badly stuck as almost no information is available on 500px developer website or on web for using this api in C#. I have reached a dead-end! Following is my code:-
1.]For requesting token and authorizing user -
string normalizedUrl;
string normalizedRequestParameters;
OAuth.OAuthBase myOAuth = new OAuth.OAuthBase();
try
{
Uri uri = new Uri("https://api.500px.com/v1/oauth/request_token");
string consumerKey = "u26X4av9ydNPd7kteT7bunfcdjHqVttYWIDOC1lA";
string consumerSecret = "73iaFPqCR4xkH3dgYIcPauTqhI6tMHWChDivnOP7";
string timeStamp = myOAuth.GenerateTimeStamp();
string nonce = myOAuth.GenerateNonce();
myOAuth.includeVersion = true;
string signature = myOAuth.GenerateSignature(uri, consumerKey, consumerSecret,
"", "", "GET", timeStamp, nonce, OAuth.OAuthBase.SignatureTypes.HMACSHA1,
out normalizedUrl, out normalizedRequestParameters);
string authorizationUrl = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + myOAuth.UrlEncode(signature);
Uri signInUrl = new Uri(authorizationUrl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(signInUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stIn = new StreamReader(response.GetResponseStream());
string responseString = stIn.ReadToEnd();
stIn.Close();
//oauth_token=cf40227bb7ede4d6e56ff790324761b3&oauth_token_secret=0bcb59dff2c1d095739c86c534fc62d7ed224fecfe8744d48c9c95f36211382f
if (responseString.Contains("oauth_token=") && responseString.Contains("oauth_token_secret="))
{
String RespToken = responseString.Split('&')[0].Replace("oauth_token=", "");
String RespSecret = responseString.Split('&')[1].Replace("oauth_token_secret=", "");
uri = new Uri("https://api.500px.com/v1/oauth/authorize");
timeStamp = myOAuth.GenerateTimeStamp();
nonce = myOAuth.GenerateNonce();
myOAuth.includeVersion = true;
signature = myOAuth.GenerateSignature(uri, consumerKey, consumerSecret, RespToken
, RespSecret, "GET", timeStamp, nonce, OAuth.OAuthBase.SignatureTypes.HMACSHA1,
out normalizedUrl, out normalizedRequestParameters);
Console.WriteLine("Signature=="+signature);
authorizationUrl = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + myOAuth.UrlEncode(signature);
Uri signInUrl1 = new Uri(authorizationUrl);
webBrowser1.Navigate(signInUrl1);
2.]After User clicks on Authorise this application for getting access_token-
private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
string parameters;
string normalizedUrl;
string normalizedRequestParameters;
string consumerKey = "u26X4av9ydNPd7kteT7bunfcdjHqVttYWIDOC1lA";
string consumerSecret = "73iaFPqCR4xkH3dgYIcPauTqhI6tMHWChDivnOP7";
OAuth.OAuthBase myOAuth = new OAuth.OAuthBase();
try
{
if (e.Url.ToString().Contains("https://www.xyz.com/"))
{
String url = (e.Url.ToString()).Replace("https://www.xyz.com/?","");
if( url.Contains("oauth_token="))
{
string OAuthToken = url.Split('&')[0].Replace("oauth_token=", "");
var uri = "https://api.500px.com/v1/oauth/access_token";
OAuthBase oAuth = new OAuthBase();
var nonce = oAuth.GenerateNonce();
var timeStamp = oAuth.GenerateTimeStamp();
var signature = oAuth.GenerateSignature(new Uri(uri), consumerKey, consumerSecret,
OAuthToken, String.Empty, "POST", timeStamp, nonce,
OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters);
signature = HttpUtility.UrlEncode(signature);
var requestUri = normalizedUrl + "?" + "oauth_callback=https://www.xyz.com" +"?"+ normalizedRequestParameters + "&oauth_signature=" + myOAuth.UrlEncode(signature);
Console.WriteLine(requestUri);
var request = (HttpWebRequest)WebRequest.Create(requestUri.ToString());
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/json";
// request.ContentType = "application / x - www - form - urlencoded";
//request.Credentials = CredentialCache.DefaultCredentials;
//request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
var response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var accessToken = reader.ReadToEnd();
}
}
catch(Exception ex)
{
Console.Writeln(ex.toString());
}
}
Now following is the line where my code is breaking-
var response = request.GetResponse();
Completely at my wits end on this issue, not able to get to the root of it. Any help any directions will be highly appreciated. Any suggestions would be of great help!! Thanks a ton in advance!
@PreranaPolekar I'm not super familiar with OAuth in C#, but have you tried using this fellow's implementation: http://www.rahulpnath.com/blog/exploring-oauth-c-and-500px/ -- I don't know if it still works (but we haven't changed things on our end since then as far as OAuth goes, so it should).
@PreranaPolekar Thank you for your interest in our API! Off the bat I can see that your URL query string is misformatted: ?oauth_callback=[snip]?oauth_consumer_key=[snip]. If you could attach a log of request and response headers, that would be really helpful too!
@freeatnet, @cdmicacc first of all thanks a ton for the response!
Following are my request headers for the third step of Oauth, it is done using POST method
string OAuthToken = url.Split('&')[0].Replace("oauth_token=", "");
var uri = "https://api.500px.com/v1/oauth/access_token";
OAuthBase oAuth = new OAuthBase();
var nonce = oAuth.GenerateNonce();
var timeStamp = oAuth.GenerateTimeStamp();
var signature = oAuth.GenerateSignature(new Uri(uri), consumerKey, consumerSecret,
OAuthToken, String.Empty, "POST", timeStamp, nonce,
OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters);
signature = HttpUtility.UrlEncode(signature);
//var requestUri = normalizedUrl + "?" + "oauth_callback=https://www.picbackman.com" +"?"+ normalizedRequestParameters + "&oauth_signature=" + myOAuth.UrlEncode(signature);
//Console.WriteLine(requestUri);
var requestUri = new StringBuilder();
requestUri.AppendFormat("oauth_callback={0}&", "https://www.picbackman.com");
requestUri.AppendFormat("oauth_consumer_key={0}&", consumerKey);
requestUri.AppendFormat("oauth_token={0}&", OAuthToken);
requestUri.AppendFormat("oauth_nonce={0}&", nonce);
requestUri.AppendFormat("oauth_timestamp={0}&", timeStamp);
requestUri.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
requestUri.AppendFormat("oauth_version={0}&", "1.0");
requestUri.AppendFormat("oauth_signature={0}", signature);
Console.WriteLine(requestUri.ToString());
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/json";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(requestUri.ToString());
request.ContentLength = bytes.Length;
System.IO.Stream os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
var response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var accessToken = reader.ReadToEnd();
Till step two i.e making oauth/authorize request and getting the oauth_token and oauth_verifier , its working fine but I am unable to complete the third step.
I have made the first two request of oauth/authorize and oauth/request_token as GET method, can that be a issue too??
Looking forward for a favorable response, really want to get it working.
Thanks a ton in advance!!
@cdmicacc , I had seen that blog of Rahulpnath, the link which you posted, but example is for Windows 8, I need to get this working on Windows 7. Thanks a ton for the link though, i will try if I can get it working on Windows 7.
@freeatnet , yes the two question marks in the URL
?oauth_callback=[snip]?oauth_consumer_key=[snip]
I was trying various ways in forming URL and see if I could get any of them working, if I replace the second question mark with '&' ?oauth_callback=[snip]&oauth_consumer_key=[snip]> which is the correct way to do it it throws 401 Invalid OAuth Request. This was when I was making a GET request but now I am sending values through Parameters in POST request but still the same error of 500 Internal Server Error I am sure I am making very small mistake somewhere but after loads and loads of efforts I am unable to track it.
@PreranaPolekar, could you please gist the full HTTP trace of both attempts (as well as oAuth signature base string)?
@freeatnet , I am using Winform in my application so I cannot use HTTP trace. Is any other way I can trace it?
Hello @PreranaPolekar , did you get the Winform example working with 500px?
I am stuck at the same point.
Regards
Hi @frasalyu , First of all extremely sorry for the HUGE delay in response, checked the comment today! Sorry again. No, unfortunately I could not get it working till the end :( Where you able to crack it ?