Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'example.com', 443, @bTls, @bAutoReconnect
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    --  Configure Google service-account authentication.  Load the service account JSON key and set the
    --  required OAuth scope(s).
    DECLARE @gAuth int
    EXEC @hr = sp_OACreate 'Chilkat.AuthGoogle', @gAuth OUT

    DECLARE @sbJsonKey int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJsonKey OUT

    DECLARE @bLoaded int
    EXEC sp_OAMethod @sbJsonKey, 'LoadFile', @bLoaded OUT, 'qa_data/service_account.json'
    IF @bLoaded <> 1
      BEGIN

        PRINT 'Failed to load the service account JSON key.'
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @gAuth
        EXEC @hr = sp_OADestroy @sbJsonKey
        RETURN
      END
    DECLARE @jsonKey nvarchar(4000)
    EXEC sp_OAMethod @sbJsonKey, 'GetAsString', @jsonKey OUT
    EXEC sp_OAGetProperty @sbJsonKey, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @sbJsonKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @gAuth
        EXEC @hr = sp_OADestroy @sbJsonKey
        RETURN
      END
    EXEC sp_OASetProperty @gAuth, 'JsonKey', @jsonKey

    --  A space-delimited list of the requested permissions.
    EXEC sp_OASetProperty @gAuth, 'Scope', 'https://www.googleapis.com/auth/cloud-platform'

    --  Associate the provider so Chilkat supplies a Google bearer token on each request.
    EXEC sp_OAMethod @rest, 'SetAuthGoogle', @success OUT, @gAuth
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @gAuth
        EXEC @hr = sp_OADestroy @sbJsonKey
        RETURN
      END

    DECLARE @responseText nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'GET', '/storage/v1/b?project=my-project'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @gAuth
        EXEC @hr = sp_OADestroy @sbJsonKey
        RETURN
      END

    PRINT @responseText

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @gAuth
    EXEC @hr = sp_OADestroy @sbJsonKey


END
GO