1

Currently i'm working on api connector which is pulling marketing data from snapchat api. Everything was flawless until i've reach campaign stats endpoint with granularity "day".

Example GET url looks like:

https://adsapi.snapchat.com/v1/campaigns/some-campaign-id/stats?granularity=DAY&start_time=2017-11-05T22:00:00.000+02:00&end_time=2017-12-12T22:00:00.000+02:00

Any starting date is giving api error:

 "Invalid query parameters in request URL: [Invalid StartDateTime, 2015-11-05T22:00:00.000 02:00]"

In docs is written that date format must be ISO8601 with full hour. Even api shows how result should look like: "start_time": "2016-08-05T22:00:00.000-07:00"

I've used same date and still i had error by error. I tried to use DataTime object ((new DateTime('1-04-2018'))->format('c')), simple date('c'), manual date insert to url to see if its working and still no success.

Endpoint is working fine when i use granularity TOTAL(no dates) but that not the case here. Have somebody knows how date in url should looks like?

Marek Kaliszuk
  • 577
  • 5
  • 21
  • I've encoded + to %2B which caused timezone problem (so it's not working because api don't see my +02:00. Url used: https://adsapi.snapchat.com/v1/campaigns/xxxxx/stats?breakdown=ad&granularity=DAY&start_time=2018-04-01T00:00:00%2B02:00&end_time=2018-04-15T00:00:00%2B02:00 I've tried to use urlencode or rawencode on url but it also did not work. – Marek Kaliszuk Apr 17 '18 at 06:47
  • Possible duplicate of [Characters allowed in a URL](https://stackoverflow.com/questions/1856785/characters-allowed-in-a-url) – Ole V.V. Apr 17 '18 at 07:55

1 Answers1

1

You need to URL encode your parameters to the query. I believe your string should look something like the following.

https://adsapi.snapchat.com/v1/campaigns/some-campaign-id/stats?granularity=DAY&start_time=2017-11-05T22%253A00%253A00.000%252B02%253A000&end_time=2017-12-12T22%253A00%253A00.000%252B02%253A00

Only a very limited set of ASCII characters are allowed in URLs. Among other things, spaces are not allowed, so it has been decided that you may encode a space as a + sign. Since your times had UTC offset +02:00, the + in the offset was decoded into a space at the other end, which meant that the time string was no longer valid. You see that the start time was given as 2015-11-05T22:00:00.000 02:00 in the error message. With a space where you had put the + sign.

The characters that do not require encoding are alphanumerics [0-9a-zA-Z], special characters $-_.+!*'(),, and reserved characters used for their reserved purposes (e.g., question mark used to denote a query string). The reserved characters are ;/?:@=& and should be encoded when not used in their special meaning. Information taken from (Please) Stop Using Unsafe Characters in URLs.

Go ahead and read more about URL encoding. Your search engine will give you overwhelming amounts of reading material.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Problem is more complex and i noticed when api works and when not:$from = '2018-04-01T00:00:00.000-00:00'; $end = '2018-04-13T00:00:00.000-00:00'; //working: $from = '2018-04-04T07:00:00.000-00:00'; $end = '2018-04-16T07:00:00.000-00:00'; It seems like every hour different than 7 is not working. (or if last 4 digets are not -07:00). It looks like timezone problem. (account is set to America/LA which is for me -7. Still i don't know why it's behaving like that. – Marek Kaliszuk Apr 17 '18 at 12:43
  • Isn’t that a separate issue? Maybe ask a new question? – Ole V.V. Apr 17 '18 at 12:45