Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

PayPal: Get a sandbox access token

See more PayPal Examples

Exchange a sandbox REST app client ID and secret for a bearer token on a merchant-controlled backoffice workstation. PayPal supplies its lifetime in seconds. Store the token in a protected credential store; the companion expiry example reads only timing metadata.

PayPal documentation

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 @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  See Unlock Chilkat before making requests.
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Load these values from the credential store on the merchant backoffice workstation.
    --  Never embed the merchant secret in an app distributed to customers.
    EXEC sp_OASetProperty @http, 'Login', 'SANDBOX_CLIENT_ID'
    EXEC sp_OASetProperty @http, 'Password', 'SANDBOX_CLIENT_SECRET'
    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    DECLARE @issued int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @issued OUT

    EXEC sp_OAMethod @issued, 'SetFromCurrentSystemTime', @success OUT
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpStr', @success OUT, 'POST', 'https://api-m.sandbox.paypal.com/v1/oauth2/token', 'grant_type=client_credentials', 'utf-8', 'application/x-www-form-urlencoded', @resp
    IF @success <> 1
      BEGIN

        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT 'No HTTP response: ' + @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @issued
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
    IF (@iTmp0 < 200) or (@iTmp1 >= 300)
      BEGIN
        DECLARE @errorInfo int
        EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @errorInfo OUT

        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @errorInfo, 'Load', @success OUT, @sTmp0

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT

        EXEC sp_OAMethod @errorInfo, 'StringOf', @sTmp0 OUT, 'error'

        EXEC sp_OAMethod @errorInfo, 'StringOf', @sTmp1 OUT, 'error_description'
        PRINT 'HTTP ' + @iTmp0 + ' ' + @sTmp0 + ' ' + @sTmp1

        EXEC sp_OAMethod @errorInfo, 'StringOf', @sTmp0 OUT, 'debug_id'
        PRINT 'PayPal debug ID: ' + @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @issued
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @errorInfo
        RETURN
      END
    DECLARE @result int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT

    DECLARE @parsed int
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @result, 'Load', @parsed OUT, @sTmp0
    IF @parsed <> 1
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'Expected a JSON response; HTTP ' + @iTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @issued
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @errorInfo
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    DECLARE @accessToken nvarchar(4000)
    EXEC sp_OAMethod @result, 'StringOf', @accessToken OUT, 'access_token'
    DECLARE @lifetime int
    EXEC sp_OAMethod @result, 'IntOf', @lifetime OUT, 'expires_in'
    EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'access_token'
    IF (@iTmp0 <> 1) or (@lifetime <= 0)
      BEGIN

        PRINT 'Missing token or lifetime.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @issued
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @errorInfo
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    --  Supply accessToken to Http.AuthToken. Never log the bearer token or client secret.
    --  Save only timing metadata here, relative to the start of the token request.
    DECLARE @timing int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @timing OUT

    EXEC sp_OAMethod @issued, 'GetAsTimestamp', @sTmp0 OUT, 0
    EXEC sp_OAMethod @timing, 'UpdateString', @success OUT, 'issued_at', @sTmp0
    EXEC sp_OAMethod @timing, 'UpdateInt', @success OUT, 'expires_in', @lifetime
    DECLARE @savedTiming int
    EXEC sp_OAMethod @timing, 'WriteFile', @savedTiming OUT, 'paypal-token-timing.json'
    IF @savedTiming <> 1
      BEGIN

        PRINT 'Could not save token timing.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @issued
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @errorInfo
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @timing
        RETURN
      END

    PRINT 'Token received; lifetime in seconds: ' + @lifetime

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @issued
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @errorInfo
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @timing


END
GO