【问题标题】:Marketo rest Api create leadMarketo rest Api 创建潜在客户
【发布时间】:2017-05-17 03:29:12
【问题描述】:

我对这个创建/更新潜在客户 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。

【问题讨论】:

    标签: api marketo


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-02
        • 1970-01-01
        • 2015-03-15
        • 1970-01-01
        相关资源
        最近更新 更多