2

我对此创建/更新潜在客户 API 有疑问,http://developers.marketo.com/documentation/rest/createupdate-leads/。没有 C# 或 JAVA 的示例代码。只有红宝石可用。所以我必须自己尝试一下。但我总是从响应中得到空返回。这是我的代码:

private async Task<CreateLeadResponseResult> CreateLead(string token)
    { 

        string url = String.Format(marketoInstanceAddress+"/rest/v1/leads.json?access_token={0}", token);
        var fullUri = new Uri(url, UriKind.Absolute);
        CreateLeadResponseResult createLeadResponse = new CreateLeadResponseResult();
        CreateLeadInput input = new CreateLeadInput { email = "123@123.com", lastName = "Lee", firstName = "testtesttest", postCode = "00000" };
        CreateLeadInput input2 = new CreateLeadInput { email = "321@gagaga.com", lastName = "Lio", firstName = "ttttttt", postCode = "00000" };
        List<CreateLeadInput> inputList = new List<CreateLeadInput>();
        inputList.Add(input);
        inputList.Add(input2);

        CreateLeadRequest createLeadRequest = new CreateLeadRequest() { input = inputList };
        JavaScriptSerializer createJsonString = new JavaScriptSerializer();
        string inputJsonString = createJsonString.Serialize(createLeadRequest);

       using (var client = new HttpClient())
        {

            HttpResponseMessage response = await client.PostAsJsonAsync(fullUri.OriginalString, inputJsonString).ConfigureAwait(false);
            // I can see the JSON string is in the message body in debugging mode.

            if (response.IsSuccessStatusCode)
            {
                createLeadResponse = await response.Content.ReadAsAsync<CreateLeadResponseResult>();
            }
            else
            {
                if (response.StatusCode == HttpStatusCode.Forbidden)
                    throw new AuthenticationException("Invalid username/password combination.");
                else
                    throw new ApplicationException("Not able to get token");
            }
        }

       return createLeadResponse;}
       //get null here.

谢谢你。-C。

4

2 回答 2

1

调试此问题的最佳方法是捕获您的应用提交的确切 URL、参数和 JSON,并尝试通过 Postman(Chrome 插件)或 SOAP UI 等工具手动提交这些内容。然后您会看到确切的错误消息,您可以在此处查找:http: //developers.marketo.com/documentation/rest/error-codes/。基于此,您可以更新您的代码。我对 Java 了解不多,但这就是我让 Python 代码工作的方式。

于 2015-04-26T16:26:31.920 回答
0

您的示例代码对我自己的实现非常有帮助。谢谢!

玩了一会儿之后,我意识到这JavaScriptSerializer一步是不必要的,因为PostAsJsonAsync它会自动序列化你传递给它的任何对象。双重序列化阻止 Marketo 的 API 处理输入。

另外,我同意 Jep 的观点,即 Postman 非常有帮助。但是在出现此错误的情况下,Postman 工作正常(使用 的内容inputJsonString),但我的 C# 代码仍然无法正常工作。所以我临时修改了代码以返回一个dynamic对象而不是一个CreateLeadResponseResult. 在调试模式下,这使我可以看到由于不适合CreateLeadResponseResult类型而被丢弃的字段,这使我得到了上面的解决方案。

于 2015-06-05T18:21:17.417 回答