1

我正在尝试使用 Dwolla API 启动并运行,但是当我尝试注册新用户时,它返回 500 内部服务器错误并且没有错误消息,这令人沮丧,因为它没有给我任何东西。

这是代码:

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 但也出错了。

4

1 回答 1

1

我忘记将请求的 Content-Type 设置为 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);
            }
        }
于 2012-12-15T17:57:07.530 回答