Xbasic

a5_getAuthTokensFromAuthTable Function

Syntax

a5_getAuthTokensFromAuthTable as p (C namedresource [, L flagRefreshTokenIfExpired [, L flagSaveRefreshedTokenInAuthenticationKeysTable]])

Arguments

namedresourceCharacter

A named resource provider. See Resource Providers for more information.

flagRefreshTokenIfExpiredLogical

Default = .t..

flagSaveRefreshedTokenInAuthenticationKeysTableLogical

Default = .t..

Returns

resultPointer

Returns an object with the following properties:

errorLogical

.f. if no error occurred. .t. if an error occurred.

errorTextCharacter

If error is .t., contains additional information about the error.

Access_tokenCharacter

The access token.

This property only exists if error is .f..

Access_token_expiresCharacter

When the token expires.

This property only exists if error is .f..

Access_token_secretCharacter

The access token secret.

This property only exists if error is .f..

oauth_versionCharacter

The oAuth version.

This property only exists if error is .f..

refresh_tokenCharacter

The refresh token.

This property only exists if error is .f..

ResourceURLCharacter

The resource URL.

This property only exists if error is .f..

scopesCharacter

The scopes for the token.

This property only exists if error is .f..

expiredLogical

Whether or not the token is expired. .t. if expired, otherwise .f..

This property only exists if error is .f..

wasRefreshedLogical

Whether or not the token was refreshed. .t. if refreshed, otherwise .f..

This property only exists if error is .f..

Description

Retrieves the access and refresh token from authenticationKeys table for a named resource provider. If the access token has expired and a refresh token is available, token can be refreshed and stored in the authenticationKeys table

Discussion

When you complete an oAuth flow to authorize an external resource such as Salesforce, you can specify that the tokens returned should be stored in a sql table (see Project Properties Authentication Keys table).

After you have performed an initial authentication for a named resource, the Authentication Keys table will contain a record for the specified named provider.

The a5_getAuthTokensFromAuthTable() function will query the Authentication Keys table and it will return an object with the access token for the service so that you can make an API call.

If the access token has expired, and it a refresh token is available, the function will automatically get a new access token and write this new token to the Authentication Keys table.

Using this function, it is possible to bypass the oAuth authentication flow that would normally be required before making an API call, as long as a prior authentication flow has been completed and a record has been written to the Authentication Keys table.

Example:

dim p as p

'get tokens for the "salesforce" named provider
p = a5_getAuthTokensFromAuthTable("salesforce")

If there is no record in the Authentication Keys table, the p object will contain:

{
     "error": true,
     "errorText": "token not found"
}

If a record was found in the Authentication Keys table, the p object will contain:

{
     "Access_token": "your token",
     "Access_token_expires": "20220409115123124",
     "Access_token_secret": "",
     "oauth_version": "2.0",
     "refresh_token": "your refresh token",
     "ResourceURL": "you resource URL",
     "scopes": "",
     "expired": false,
     "error": false,
     "wasRefreshed": false,
     "errorText": ""
}

The refresh_token will only be present if the initial authentication requested a refresh_token. This is typically specified in the scopes parameter.

The wasRefreshed property will be true if the access_token had expired and the refresh_token had to be used to refresh the access token.

See Also