3

I am a complete beginner to both iOS dev and working with Google's APIs. I'm learning both of them simultaneously so I apologize if this is a newbie question.

The Parsing Error has been "repealed & replaced" with a new API error below

I am trying to write an app that will tell me, that at the time of API request, whether the calendar is in the middle of an event or not.

I tried using this answer posted on a similar question.

My code looks like this now: (LOOK AT BOTTOM FOR UPDATES)

let json = "{ \"timeMin\": \(currentDate), \"timeMax\": \(currentDate)}"
urlString = "https://www.googleapis.com/calendar/v3/freeBusy"


let url = URL(string: urlString)!
let jsonData = json.data(using: .utf8, allowLossyConversion: false)!

var request = URLRequest(url: url)
request.httpMethod = HTTPMethod.post.rawValue
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData

Alamofire.request(request).responseJSON {
    (response) in
    print (response)
    }

And i get the following output: (Note: This is fixed by specifying in the json parameter which calendar to retrieve info from)

SUCCESS: {
    error =     {
        code = 400;
        errors =         (
                        {
                domain = global;
                message = "Parse Error";
                reason = parseError;
            }
        );
        message = "Parse Error";
    };
}

I am unsure how to proceed from here- also, is making a freebusy POST request even the best way to go about solving my problem?


UPDATE:

So I recognized that in my body parameter, I was not specifying which calendar to request information from: I made the following adjustments:

    var body : Parameters  = [
    "timeMin": dateString,
    "timeMax": dateString,
    "items": [
        [
            "id": CALENDAR_ID
        ]]
    ]

    Alamofire.request(url, method: HTTPMethod.post, parameters: body, encoding: JSONEncoding.default).responseJSON {
            (response) in
                print(response)
    }
}

However, now I get the following error:

SUCCESS: {
    error =     {
        code = 403;
        errors =         (
                        {
                domain = usageLimits;
                extendedHelp = "https://code.google.com/apis/console";
                message = "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.";
                reason = dailyLimitExceededUnreg;
            }
        );
        message = "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.";
    };
}
Community
  • 1
  • 1
degenPenguin
  • 725
  • 1
  • 8
  • 23

1 Answers1

2

So I figured both problems out:

FIRST- PARSE ERROR

This error, in the context of freebusy came from my POST parameter not specifying which calendars to retrieve information from

THE SOLUTION: (crucial part is bolded)

var body : Parameters  = [
    "timeMin": dateString,
    "timeMax": dateString,
    "items": [                //make sure you have this (plus next 4 lines)
        [
           "id": CALENDAR_ID
        ]
    ]
]

SECOND- UNAUTHENTICATED USAGE LIMIT ERROR

I solved this with help from this question.

In a nutshell, all of the discovery-based APIs require your calls to be identified. The error you're getting is what I usually see when a Google API can't identify your app. You can usually make a handful of unidentified calls, but you'll almost certainly run into the much-lower "unidentified" quota pretty quickly. You can either authenticate with OAuth (which identifies your app) or you can specify an API key (which identifies your app) or you can do both.

SOLUTION

I solved this by appending the API key to my URL as follows (don't click):

https://www.googleapis.com/calendar/v3/freeBusy?keys=PASTEKEYHERE

Community
  • 1
  • 1
degenPenguin
  • 725
  • 1
  • 8
  • 23