簡體   English   中英

500 px API與C#引發500內部服務器錯誤

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


我正在嘗試使用C#實現500px API 我可以使用500px API驗證用戶身份,但無法獲得access_token來交換response_token ,這使我的Oauth 1.0第三步不完整。 我能夠授權用戶並獲得oauth_tokenoauth_verifier,但是當我使用此oauth_token發出以下請求時:-

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


500內部服務器錯誤以及以下屏幕被拋出

在此處輸入圖片說明

我已經調試了數千次代碼,嘗試了各種方法來形成URL,向請求中添加了各種參數,但沒有幫助。 我非常難受,因為在500px開發人員網站上或在C#中使用此api的網站上幾乎沒有可用的信息。 我已經死胡同了!
以下是我的代碼:

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.]用戶單擊“授權此應用程序以獲取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());
            }
            }


現在,下面是我的代碼中斷的行:-

         var response = request.GetResponse();

在這個問題上,我完全無能為力,無法找到根源。 任何方向的幫助將不勝感激。 任何建議都會有很大幫助!!
在此先感謝一噸!

這是做什么的:

  1. 使用GET而不是POST(POST可能會起作用;我尚未嘗試過,但500px和POST的成功很少)

  2. 在查詢URL中包括在授權步驟中收到的oauth_verifier

  3. 使用您的使用者密鑰對整個查詢簽名並請求令牌密鑰(在步驟1中返回)

HTTP回復正文中應返回“ oauth_token = ...&oauth_token_secret = ...”的代碼段。

確保您的OAuth實現不會刪除任何以“ oauth_”開頭的參數! 我的是,它正在剝離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