Hi guys,
I'm trying to get Interesting Moments acitivities using the REST API. But the json response is not returning Results, can you guys help me please?
The activityType is 46, I have requested the types previously.
String url = host + "/rest/v1/activities.json?access_token=" + getToken() + "&activityTypeIds=" + activityType + "&nextPageToken=" + nextPageToken;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
request.Accept = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
var result = reader.ReadToEnd();
And this is the response:
{"requestId":"271a#1632ca3a6a1","success":true,"nextPageToken":"4GAX7YNCIJKO2VAED5LH5PQIYO46Y4DV52DUUR5BJRFEATLBK2MA====","moreResult":true}
getToken()
private String getToken()
{
String url = host + "/identity/oauth/token?grant_type=client_credentials&client_id=" + clientId + "&client_secret=" + clientSecret;
String result = getData(url);
Dictionary<String, String> dict = JsonConvert.DeserializeObject<Dictionary<String, String>>(result);
return dict["access_token"];
}
nextPageToken
String pagingTokenObj = getPagingToken("2017-01-01T00:00:00-00:00");
private String getPagingToken(string sinceDatetime)
{
String url = host + "/rest/v1/activities/pagingtoken.json?access_token=" + getToken() + "&sinceDatetime=" + sinceDatetime;
String result = getData(url);
return result;
}
getData()
private String getData(string url)
{
String data = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
request.Accept = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
var result = reader.ReadToEnd();
if (response.StatusCode.ToString() == "OK")
{
data = result;
}
else
{
Console.WriteLine(response.StatusCode.ToString());
data = "Status:" + response.StatusCode.ToString();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return data;
}
Solved! Go to Solution.
Hi Sandford,
Thanks, I found out here (Get Lead Activities REST API is not returning any activities) that you must provide a close datestamp to get results. Since I put a very old date I didn't get any results. I changed it to this month and the 'result' came.
Thank you!
Hi Sanford,
I have updated the post adding the calls. About your second point, if I'm evaluating the 'results' response and it doesn't exist, I will receive an error, so at least the 'results' response should appear as empty, right?
Regards,
Raul
if I'm evaluating the 'results' response and it doesn't exist, I will receive an error, so at least the 'results' response should appear as empty, right?
You can't have your code be conditional only on the presence of the results property. It needs to check success and moreResult.
Hi Sandford,
Thanks, I found out here (Get Lead Activities REST API is not returning any activities) that you must provide a close datestamp to get results. Since I put a very old date I didn't get any results. I changed it to this month and the 'result' came.
Thank you!
But your code can't depend the presence of results -- that's not how the API works. It's correctly telling you that there are more pages to come, even if the current page is empty, and you can't guarantee that won't happen even with a seemingly recent date.
I don't understand why you can't interpret the API response as documented?