Sample code for 30+ languages & platforms
DataFlex

Configure Google Service Account Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthGoogle, which associates an AuthGoogle provider so Chilkat supplies a Google OAuth 2.0 bearer token on each request. The provider is configured with a service account JSON key and the required OAuth scope.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The AuthGoogle provider performs a service-account (server-to-server) OAuth flow. For interactive, browser-based user consent, the OAuth2 provider is used instead.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Variant vGAuth
    Handle hoGAuth
    Handle hoSbJsonKey
    Boolean iBLoaded
    String sJsonKey
    String sResponseText
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End
    Move True To iBTls
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "example.com" 443 iBTls iBAutoReconnect To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Configure Google service-account authentication.  Load the service account JSON key and set the
    //  required OAuth scope(s).
    Get Create (RefClass(cComChilkatAuthGoogle)) To hoGAuth
    If (Not(IsComObjectCreated(hoGAuth))) Begin
        Send CreateComObject of hoGAuth
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJsonKey
    If (Not(IsComObjectCreated(hoSbJsonKey))) Begin
        Send CreateComObject of hoSbJsonKey
    End
    Get ComLoadFile Of hoSbJsonKey "qa_data/service_account.json" To iBLoaded
    If (iBLoaded <> True) Begin
        Showln "Failed to load the service account JSON key."
        Procedure_Return
    End

    Get ComGetAsString Of hoSbJsonKey To sJsonKey
    Get ComLastMethodSuccess Of hoSbJsonKey To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSbJsonKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComJsonKey Of hoGAuth To sJsonKey

    //  A space-delimited list of the requested permissions.
    Set ComScope Of hoGAuth To "https://www.googleapis.com/auth/cloud-platform"

    //  Associate the provider so Chilkat supplies a Google bearer token on each request.
    Get pvComObject of hoGAuth to vGAuth
    Get ComSetAuthGoogle Of hoRest vGAuth To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComFullRequestNoBody Of hoRest "GET" "/storage/v1/b?project=my-project" To sResponseText
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sResponseText


End_Procedure