Custom Credentials
Desktop Authentication with a Custom OAuth App
Follow the steps below to authenticate with the credentials for a custom OAuth app. See Creating a Custom OAuth App.Get and Refresh the OAuth Access Token
After setting the following, you are ready to connect:- AuthScheme: Set this to OAuth, PKCE (if you configured a PKCE application) or OAuthClient (if you configured a custom application).
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- OAuthClientId: Set this to the client Id assigned when you registered your app.
- OAuthClientSecret: OAuth and OAuthClient only. Set this to the client secret assigned when you registered your app.
- CallbackURL: OAuth and PKCE only. Set this to the redirect URI defined when you registered your app.
When you connect the driver opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The driver then completes the OAuth process:
- Extracts the access token from the callback URL and authenticates requests.
- Obtains a new access token when it expires.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
Web Authentication with a Custom OAuth App
When connecting via a Web application, you need to register a custom OAuth app with Xero; see Creating a Custom OAuth App. You can then use the driver to get and manage the OAuth token values.This flow is only supported when AuthScheme is OAuth or PKCE. When using OAuthClient, InitiateOAuth should always be set to GETANDREFRESH so the driver can manage the authentication process. OAuthClient is a server-to-server mechnaism so the driver can authenticate without any user interaction.
Get an OAuth Access Token
Set the following connection properties to obtain the OAuthAccessToken:- AuthScheme: Set this to OAuth or PKCE if you configured a PKCE application.
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: OAuth and OAuthClient only. Set this to the client secret assigned when you registered your app.
- Scope: You may set this to a space-separated list of Xero OAuth scopes. This is recommended if your application only uses one schema, since otherwise the driver will request all available scopes.
You can then call stored procedures to complete the OAuth exchange:
-
Call the GetOAuthAuthorizationURL stored procedure. Set the AuthMode input to WEB and set the CallbackURL input to the Redirect URI you specified in your app settings.
The stored procedure returns the URL to the OAuth endpoint.
- Redirect the user to the URL, where they will log in and authorize the application. They are redirected back to the callback URL.
- Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL.
When connecting with PKCE, there is an additional PKCEVerifier value that must be passed between GetOAuthAuthorizationURL and GetOAuthAccessToken. This verifier is used by Xero to ensure that the authorization code is not intercepted and used by another application.
- Call GetOAuthAuthorizationURL as normal.
- In addition to the authroization URL, the driver will also output a PKCEVerifier value from the stored procedure. Save this value for later use.
- After the user is redirected to your callback URL, pass the saved PKCEVerifier as an input to GetOAuthAccessToken.
Refresh the OAuth Access Token
You can set InitiateOAuth to REFRESH to automatically refresh the OAuth access token when it expires, or you can call the GetOAuthAccessToken stored procedure to refresh the token manually.Automatic Refresh
To refresh the token with InitiateOAuth, set the following on the first data connection:
- AuthScheme: Set this to OAuth, or PKCE if you registerd a PKCE application.
- InitiateOAuth: Set this to REFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: OAuth only. Set this to the Client Secret in your app settings.
- OAuthAccessToken: Set this to the access token returned by GetOAuthAccessToken.
- OAuthRefreshToken: Set this to the refresh token returned by GetOAuthAccessToken.
- OAuthSettingsLocation: Set this to the path where the driver will save the OAuth values, to be persisted across connections.
On subsequent data connections, set the following:
- AuthScheme
- InitiateOAuth
- OAuthClientId
- OAuthClientSecret: OAuth only.
- OAuthSettingsLocation
Manual Refresh
You can use the GetOAuthAccessToken stored procedure to manually refresh the OAuthAccessToken. Call the stored procedure after the ExpiresIn parameter value returned by GetOAuthAccessToken has elapsed. You need to set the following connection properties:
- AuthScheme: Set this to OAuth, or PKCE if you registerd a PKCE application.
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
You can then call GetOAuthAccessToken with the following parameter set:
- OAuthRefreshToken: Set this to the OAuthRefreshToken returned by GetOAuthAccessToken.
On subsequent data connections, set the following:
- AuthScheme
- OAuthAccessToken