2012-12-15 53 views
1

我試圖啓動並運行Dwolla API,但是當我嘗試註冊新用戶時,它返回500內部服務器錯誤並且沒有錯誤消息,這是令人沮喪的,因爲它沒有給我提供任何幫助離開。Dwolla API註冊錯誤

下面是代碼:

public static bool Register(ObjectId userId, User user, out string message) { 
      var dwollaKey = ConfigurationManager.AppSettings["DwollaKey"].ToString(); 
      var dwollaSecret = ConfigurationManager.AppSettings["DwollaSecret"].ToString(); 

      //post register to dwolla api. 
      var request = new DwollaRegisterRequest { 
       client_id = dwollaKey, 
       client_secret = dwollaSecret, 
       email = user.DwollaEmailAddress, 
       password = user.DwollaPassword, 
       pin = user.DwollaPin, 
       firstName = user.FirstName, 
       lastName = user.LastName, 
       address = user.Address, 
       city = user.City, 
       state = user.State, 
       zip = user.ZipCode, 
       type = "Personal", 
       acceptTerms = "true" 
      }; 
      var requestJson = JsonConvert.SerializeObject(request); 
      var url = "https://www.dwolla.com/oauth/rest/register/"; 
      var responseJson = WebRequestHelper.PostString(url, requestJson); 
      var response = JsonConvert.DeserializeObject<DwollaRegisterResult>(responseJson); 
      message = response.Message; 

      //if success, update user with dwolla info. 
      if (response.Success) { 
       //save user. 
       var existingUser = UserService.Get(userId); 
       existingUser.DwollaId = response.Response.Id; 
       existingUser.DwollaEmailAddress = user.DwollaEmailAddress; 
       existingUser.DwollaPassword = user.DwollaPassword; 
       existingUser.DwollaPin = user.DwollaPin; 
       existingUser.FirstName = user.FirstName; 
       existingUser.LastName = user.LastName; 
       existingUser.Address = user.Address; 
       existingUser.City = user.City; 
       existingUser.State = user.State; 
       existingUser.ZipCode = user.ZipCode; 
       UserService.Save(existingUser); 
      } 
      return response.Success; 
     } 

public static string PostString(string url, string requestBody) { 
      using (WebClient client = new WebClient()) { 
       return client.UploadString(url, "POST", requestBody); 
      } 
     } 

我張貼的要求作爲一個JSON字符串,因爲我在另一個線程有人說,這就是該API期待看到的,但我也試過張貼的NameValueCollection,但那也是錯誤的。

回答

1

我忘了將請求的內容類型設置爲application/json。

public static string PostString(string url, string requestBody) { 
      using (WebClient client = new WebClient()) { 
       client.Headers.Add("Content-Type", "application/json"); 
       return client.UploadString(url, "POST", requestBody); 
      } 
     }