Swift GMail: Parse GMail REST API Error Response

Back to Index

Gets the current user's Gmail profile, but shows an error response and how to parse it..

Documentation: https://developers.google.com/gmail/api/v1/reference/users/getProfile



func chilkatTest() {
    let rest = CkoRest()
    var success: Bool

    //   Provide a previously obtained OAuth2 access token.
    let oauth2 = CkoOAuth2()
    oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
    rest.SetAuthOAuth2(oauth2)

    success = rest.Connect("www.googleapis.com", port: 443, tls: true, autoReconnect: true)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    rest.AddPathParam("userId", value: "matt@chilkat.io")

    let sbJson = CkoStringBuilder()
    success = rest.FullRequestNoBodySb("GET", uriPath: "/gmail/v1/users/userId/profile", sb: sbJson)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    if rest.ResponseStatusCode.integerValue != 200 {
        print("Received error response code: \(rest.ResponseStatusCode.integerValue)")
        print("Response body:")
        print("\(sbJson.GetAsString())")
        return
    }

    let json = CkoJsonObject()
    json.LoadSb(sbJson)

    //  The following code parses the JSON response.
    //  A sample JSON response is shown below the sample code.
    var errorCode: Int
    var errorMessage: String?
    var i: Int
    var count_i: Int
    var domain: String?
    var reason: String?
    var message: String?
    var extendedHelp: String?

    errorCode = json.IntOf("error.code").integerValue
    errorMessage = json.StringOf("error.message")
    i = 0
    count_i = json.SizeOfArray("error.errors").integerValue
    while i < count_i {
        json.I = i
        domain = json.StringOf("error.errors[i].domain")
        reason = json.StringOf("error.errors[i].reason")
        message = json.StringOf("error.errors[i].message")
        extendedHelp = json.StringOf("error.errors[i].extendedHelp")
        i = i + 1
    }

    print("Example Completed.")

}

Sample JSON Response Body

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured. Gmail API has not been used in project 377697329735 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=377697329735 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "extendedHelp": "https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=377697329735"
   }
  ],
  "code": 403,
  "message": "Access Not Configured. Gmail API has not been used in project 377697329735 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=377697329735 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."
 }
}