2014-06-19 48 views
0


我試圖實現500px API與C#。我能夠使用500像素API對用戶進行身份驗證,但我無法獲取access_token作爲回報response_token這使我的第三步Oauth 1.0不完整。我能夠授權用戶,並得到的oauth_tokenoauth_verifier但是當我使用這個的oauth_token作出以下要求: - 與下面的屏幕相處
500 px api with C#throwing 500內部服務器錯誤

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


500內部服務器錯誤拋出

enter image description here

我有調試我的代碼,如千次,嘗試不同的方式來形成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(); 

完全在我的智慧結束在這個問題上,沒有能夠得到它的根。任何幫助任何方向將不勝感激。任何建議將有很大的幫助!
感謝提前一噸!

+0

我一直在努力處理500px以及完全不準確的文檔。您的代碼在這裏幫助我至少進入第二階段。我的直覺是,鑑於文檔在GET工作時錯誤地使用POST,我認爲這可能是最後一步,也是一個GET。將發佈進展! –

回答

0

這裏是做什麼:

  1. 使用GET而不是POST(POST可以正常工作;我還沒有嘗試過,並沒有取得多少成功與500像素,並張貼)

  2. 附上oauth_verifier您在查詢URL授權步驟

  3. 註冊使用消費者密鑰整個查詢接收請求令牌祕密(步驟1中返回)

應該返回的代碼片段「oauth_token = ... & oauth_token_secret = ...「在HTTP應答體。

確保您的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); 
+0

嗨@Chris,謝謝你的回答。我幾乎已經放棄了這一點,但肯定會嘗試你的方式,這個週末左右,並會接受答如果它對我有用的話。快樂編碼:) – Prerana