DataFlex 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


Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoRest
    Boolean iSuccess
    Variant vOauth2
    Handle hoOauth2
    Variant vSbJson
    Handle hoSbJson
    Handle hoJson
    Integer iErrorCode
    String sErrorMessage
    Integer i
    Integer iCount_i
    String sDomain
    String sReason
    String sMessage
    String sExtendedHelp
    String sTemp1
    Integer iTemp1

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    //   Provide a previously obtained OAuth2 access token.
    Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
    If (Not(IsComObjectCreated(hoOauth2))) Begin
        Send CreateComObject of hoOauth2
    End
    Set ComAccessToken Of hoOauth2 To "OAUTH2_ACCESS_TOKEN"
    Get pvComObject of hoOauth2 to vOauth2
    Get ComSetAuthOAuth2 Of hoRest vOauth2 To iSuccess

    Get ComConnect Of hoRest "www.googleapis.com" 443 True True To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddPathParam Of hoRest "userId" "matt@chilkat.io" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJson
    If (Not(IsComObjectCreated(hoSbJson))) Begin
        Send CreateComObject of hoSbJson
    End
    Get pvComObject of hoSbJson to vSbJson
    Get ComFullRequestNoBodySb Of hoRest "GET" "/gmail/v1/users/userId/profile" vSbJson To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "Received error response code: " iTemp1
        Showln "Response body:"
        Get ComGetAsString Of hoSbJson To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get pvComObject of hoSbJson to vSbJson
    Get ComLoadSb Of hoJson vSbJson To iSuccess

    //  The following code parses the JSON response.
    //  A sample JSON response is shown below the sample code.

    Get ComIntOf Of hoJson "error.code" To iErrorCode
    Get ComStringOf Of hoJson "error.message" To sErrorMessage
    Move 0 To i
    Get ComSizeOfArray Of hoJson "error.errors" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJson To i
        Get ComStringOf Of hoJson "error.errors[i].domain" To sDomain
        Get ComStringOf Of hoJson "error.errors[i].reason" To sReason
        Get ComStringOf Of hoJson "error.errors[i].message" To sMessage
        Get ComStringOf Of hoJson "error.errors[i].extendedHelp" To sExtendedHelp
        Move i + 1 To i
    Loop

    Showln "Example Completed."


End_Procedure

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."
 }
}