简体   繁体   English

500 px API与C#引发500内部服务器错误

[英]500 px api with C# throwing 500 Internal Server Error


I am trying to implement 500px API with C# . 我正在尝试使用C#实现500px API 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. 我可以使用500px API验证用户身份,但无法获得access_token来交换response_token ,这使我的Oauth 1.0第三步不完整。 I am able to authorize user and get oauth_token and oauth_verifier but when I use this oauth_token for making following request :- 我能够授权用户并获得oauth_tokenoauth_verifier,但是当我使用此oauth_token发出以下请求时:-

       https://api.500px.com/v1/oauth/access_token


500 Internal Server Error along with the following screen gets thrown 500内部服务器错误以及以下屏幕被抛出

在此处输入图片说明

I have debugged my code like thousand times, tried different ways to form URL, added various parameters to the request but no help. 我已经调试了数千次代码,尝试了各种方法来形成URL,向请求中添加了各种参数,但没有帮助。 I am very badly stuck as almost no information is available on 500px developer website or on web for using this api in C#. 我非常难受,因为在500px开发人员网站上或在C#中使用此api的网站上几乎没有可用的信息。 I have reached a dead-end! 我已经死胡同了!
Following is my code:- 以下是我的代码:

1.]For requesting token and authorizing user :- 1.]用于请求令牌并授权用户:-

        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:- 2.]用户单击“授权此应用程序以获取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! 在此先感谢一吨!

Here's what to do: 这是做什么的:

  1. Use GET rather than POST (POST may work; I've not tried it and have had little success with 500px and POSTing) 使用GET而不是POST(POST可能会起作用;我尚未尝试过,但500px和POST的成功很少)

  2. Include the oauth_verifier you received during the authorisation step in the query URL 在查询URL中包括在授权步骤中收到的oauth_verifier

  3. Sign the entire query using your consumer key and request token secret (returned in step 1) 使用您的使用者密钥对整个查询签名并请求令牌密钥(在步骤1中返回)

Code snippet which should return "oauth_token=...&oauth_token_secret=..." in the HTTP reply body. HTTP回复正文中应返回“ oauth_token = ...&oauth_token_secret = ...”的代码段。

Make sure your OAuth implementation doesn't strip out any parameters beginning with "oauth_"! 确保您的OAuth实现不会删除任何以“ oauth_”开头的参数! Mine was and it was stripping out the oauth_verifier parameter which is required by 500px. 我的是,它正在剥离500像素所需的oauth_verifier参数。

        OAuth.OAuthBase oauth = new OAuth.OAuthBase();

        string strUrl = "";
        string strParams = "";

        string signature = oauth.GenerateSignature(new Uri(API_URL + "oauth/access_token?oauth_verifier=" + this.oauthVerifier),
                                            this.consumerKey, this.consumerSecret, this.oauthToken, this.requestTokenSecret,
                                            "GET", oauth.GenerateTimeStamp(), oauth.GenerateNonce(),
                                            OAuth.OAuthBase.SignatureTypes.HMACSHA1,
                                            out strUrl, out strParams);

        string authorizationUrl = strUrl + "?" + strParams + "&oauth_signature=" + System.Web.HttpUtility.UrlEncode(signature);
        Debug.WriteLine("url>" + authorizationUrl);

        Response reply = SendGetRequest(authorizationUrl);
        if (reply.Success)
        {
            Debug.WriteLine("access_token>" + reply.Content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM