Establishing a Connection
The objects available within our connector are accessible from the "cdata.netsuite" module. To use the module's objects directly:
- Import the module as follows:
import cdata.netsuite as mod
- To establish a connection string, call the connect() method from the connector object using an appropriate connection string, such as:
mod.connect("AccountId=XABC123456;Schema=SuiteTalk;AuthScheme=Token;OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;OAuthAccessToken=MyOAuthAccessToken;OAuthAccessTokenSecret=MyOAuthAccessTokenSecret;")
Connecting to NetSuite
NetSuite currently offers two distinct APIs:- SuiteTalk is the older SOAP-based service we use to communicate with NetSuite. It has broad support for a lot of entities and full support for INSERT/UPDATE/DELETE. However, the tools offered for selecting data are fairly weak, which yields very poor performance during a SELECT. There is not a great way to join tables. Grouping and aggregating data is unavailable with this API, which means to support them they must be done entirely client side.
- SuiteQL is the newer API. It offers a SQL-like method of communicating with the service, which allows for more rich JOIN support, as well as GROUP BY and aggregations. It also fully supports retrieving only the columns you want to select. This makes it much more performant than SuiteTalk for selecting data. However, it only supports selecting data.
To connect to NetSuite, you must:
- Set Schema to specify the API you are using to connect.
If you are just retrieving data, we recommend you use SuiteQL. If you need to both retrieve and modify data, we recommend you use SuiteTalk. - Set the appropriate connection options for the API in use. (Be aware that each API has different available connection options, as described in Permission Configurations.)
- If you plan to connect to SuiteTalk, we recommend that you specify the NetsuiteMetadataFolder. The Netsuite Metadata Folder is a folder where NetSuite metadata files are stored. If you do not specify one, a folder location is selected automatically. To ensure fast load times when listing metadata about tables, it is best to set this property.
- Ensure that any connecting user has permissions on the specified AccountId to connect through NetSuite Web services. This property is required for both OAuth 1.0 (Token-Based Authentication) and OAuth 2.0. For details about required and optional permissions, see Permission Configurations.
To create or edit a role for Web services permissions:
- Log into NetSuite.
- If you need to create a new role, navigate to Setup > User/Roles > Manage Roles > New.
If you need to modify an existing role, navigate to Setup > User/Roles > Manage Roles and select the role to be edited. - Click Permissions > Setup.
- Add permissions for SOAP Web Services and REST Web Services.
- Add other permissions that are needed for interacting with various entities and transactions. (See Permission Configurations.)
- Under Setup, navigate to User/Roles > Manage Users. Select the user you just created or edited.
- At the Access tab, add the newly-created role.
- Save the new or changed user.
Authenticating to NetSuite
This section describes how to authenticate to NetSuite from earlier versions of SuiteTalk (which use Basic authentication), later versions of SuiteTalk, and all versions of Suite QL.
Suite Talk 2020.2 or Earlier
As of 2020.2, NetSuite no longer supports Basic (user/password) authentication. While the CData Python Connector for NetSuite continues to support User/Password connections for Version set to values lower than 2020.2, we recommend that all customers migrate to the OAuth and Token Based Authentication mechanisms described below.
To continue using Basic authentication with an early version of SuiteTalk, set the AuthScheme to Basic to support user/password credentials. However, you must also manually specify a lower Version to use it.
To support either Token-Based or OAuth 2.0 authentication, set the AuthScheme to OAuth.
Suite Talk or Suite QL
NetSuite offers two forms of OAuth authentication:- Token Based Authentication (TBA) is essentially OAuth 1.0 with the OAuthAccessToken and OAuthAccessTokenSecret created within the NetSuite UI instead of at runtime. TBA is available for both SuiteTalk versions later than 2020.2, and SuiteQL.
- OAuth 2.0 Authentication is only available for SuiteQL. To enforce OAuth 2.0 authentication, you can do one of the following:
- Set OAuthVersion explicitly to the API in use, or
- Set Schema to SuiteQL.
Note: Be aware that if you are using SuiteTalk, you will not be able to use OAuth 2.0 even if you configure the OAuthVersion to SuiteTalk. SuiteTalk does not support OAuth 2.0.
To support either Token-Based or OAuth 2.0 authentication, set the AuthScheme to OAuth.
Token Based Authentication (OAuth 1.0)
Token Based Authentication (TBA) is essentially OAuth 1.0. You can use TBA with either SuiteTalk or the SuiteQL Schema.
Token Based Authentication is performed by creating the OAuthClientId, OAuthClientSecret, OAuthAccessToken and OAuthAccessTokenSecret directly within the NetSuite UI by an administrator with permissions to do so.
To support the use of TBA, set the AuthScheme to Token and create tokens in the NetSuite UI, as described below.
Creating Tokens in the NetSuite UI
Many NetSuite admins prefer to create and assign a token directly in the NetSuite UI. Connecting via token enables users to bypass the normal steps for generating an OAuth Access Token. This provides the admin with more direct control over giving access. However, it also creates a need for them to take manual steps in the UI each time they need to create a new token.
Instead of creating and assigning a token directly in the NetSuite UI, follow this procedure:
- In NetSuite, log in using an account with administrator privileges.
- Navigate to Setup > Company > Enable Features > SuiteCloud > Manage Authentication.
- Select Token-Based Authentication and TBA: Authorization Flow.
- Save changes.
- Navigate to Setup > Integration > Manage Integrations.
- Create a new integration and select Token-Based Authentication.
When the integration is created, it displays the Consumer Key and Consumer Secret. These map directly to the OAuthClientId and OAuthClientSecret connection properties. - Record the values for Consumer Key and Consumer Secret.
- To create a new token role or modify an existing role, navigate to Setup > User/Roles > Manage Roles.
- Navigate to Permissions > Setup.
- Assign the role User Access Token: Full, Access Token Management: Full, and Web Services: Full permissions.
- To add the new token role to a user who is listed in Lists > Employees > Employees, select the employee, and then navigate to Access > Roles.
- Navigate to Setup > User/Roles > Access Tokens and create a new access token.
- For application name, specify the integration that was created earlier. Assign this to the same user and role that was created or updated in the previous steps.
- After creating the access token, a Token Id and Token Secret are displayed. These map directly to the OAuthAccessToken and OAuthAccessTokenSecret. Write them down.
After the access token is created, it can be used to connect using the values obtained from the previous steps. Before you connect, specify at least the following connection properties:
- AccountId = the account to connect to.
- OAuthClientId = the Consumer Key displayed when the application was created.
- OAuthClientSecret = the Consumer Secret displayed when the application was created.
- OAuthAccessToken = the Token Id when the access token was created.
- OAuthAccessTokenSecret = the Token Secret when the access token was created.
OAuth 2.0
NetSuite SuiteTalk supports OAuth 2.0 authentication. To enable this authentication from all OAuth flows, you must set AuthScheme to OAuth. Additionally, the AccountId is required to specify the account being connected to. If you want to authenticate via a Web application, you must create a custom OAuth application, as described in Creating a Custom OAuth Application.The following subsections describe how to authenticate to NetSuite from three common authentication flows:
- Desktop: a connection to a server on the user's local machine, frequently used for testing and prototyping. Authenticated via either embedded OAuth or custom OAuth.
- Web: access to data via a shared website. Authenticated via custom OAuth only.
- Headless Server: a dedicated computer that provides services to other computers and their users, which is configured to operate without a monitor and keyboard. Authenticated via embedded OAuth or custom OAuth.
For information about how to create a custom OAuth application, and why you might want to create one even for auth flows that have embedded OAuth credentials, see Creating a Custom OAuth Application.
For a complete list of connection string properties available in NetSuite, see Connection.
Desktop Applications
CData provides an embedded OAuth application that simplifies authentication at the desktop. You can also authenticate from the desktop via a custom OAuth application, which you configure and register at the NetSuite console. For further information, see Creating a Custom OAuth Application.Before you connect, set the following variables:
- AccountId = the account to connect to.
- InitiateOAuth = GETANDREFRESH. Used to automatically get and refresh the OAuthAccessToken.
- Custom OAuth applications only:
- OAuthClientId = the client Id assigned when you registered your custom OAuth application.
- OAuthClientSecret = the client secret assigned when you registered your custom OAuth application.
- CallbackURL = the redirect URI defined when you registered your custom OAuth application.
When you connect, the connector opens NetSuite's OAuth endpoint in your default browser. Log in and grant permissions to the application.
After you grant permissions to the application, the connector completes the OAuth process:
- The connector obtains an access token from NetSuite and uses it to request data.
- The OAuth values are saved in the path specified in OAuthSettingsLocation. These values persist across connections.
When the access token expires, the connector refreshes it automatically.
Web Applications
Authenticating via the Web requires you to create and register a custom OAuth application with NetSuite, as described in Creating a Custom OAuth Application. You can then use the connector to get and manage the OAuth token values.This section describes how to get the OAuth access token, how to have the driver refresh the OAuth access token automatically, and how to refresh the OAuth access token manually.
Get the OAuth access token:
- Set the following connection properties to obtain the OAuthAccessToken:
- OAuthClientId = the client Id in your application settings.
- OAuthClientSecret = the client secret in your application settings.
- Call stored procedures to complete the OAuth exchange:
- Call the GetOAuthAuthorizationURL stored procedure. Set the AuthMode input to WEB and the CallbackURL to the Redirect URI you specified in your application settings. The stored procedure returns the URL to the OAuth endpoint.
- Navigate to the URL that the stored procedure returned in Step 1. Log in and authorize the web application. You 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 redirect URI.
After you obtain the access and refresh tokens, you can connect to data and refresh the OAuth access token automatically.
Automatic refresh of the OAuth access token:
To have the connector automatically refresh the OAuth access token, do the following:
- The first time you connect to data, set the following connection parameters:
- AccountId = the account to connect to.
- InitiateOAuth = REFRESH.
- OAuthClientId = the client Id in your application settings.
- OAuthClientSecret = the client secret in your application settings.
- OAuthAccessToken = the access token returned by GetOAuthAccessToken.
- OAuthSettingsLocation = the path where you want the connector to save the OAuth values, which persist across connections.
- On subsequent data connections, set the following:
- InitiateOAuth
- OAuthSettingsLocation
Manual refresh of the OAuth access token:
The only value needed to manually refresh the OAUth access token is the OAuth refresh token.
- To manually refresh the OAuthAccessToken after the ExpiresIn period (returned by GetOAuthAccessToken) has elapsed, call the RefreshOAuthAccessToken stored procedure.
- Set the following connection properties:
- AccountId = the account to connect to.
- OAuthClientId = the Client Id in your application settings.
- OAuthClientSecret = the Client Secret in your application settings.
- Call RefreshOAuthAccessToken with OAuthRefreshToken set to the OAuth refresh token returned by GetOAuthAccessToken.
- After the new tokens have been retrieved, set the OAuthAccessToken property to the value returned by RefreshOAuthAccessToken. This opens a new connection.
Store the OAuth refresh token so that you can use it to manually refresh the OAuth access token after it has expired.
Headless Machines
If you need to log in to a resource that resides on a headless machine, you must authenticate on another device that has an internet browser. You can do this in either of the following ways:
- Option 1: Obtain the OAuthVerifier value.
- Option 2: Install the connector on a machine with an internet browser and transfer the OAuth authentication values after you authenticate through the usual browser-based flow.
After you execute either Option 1 or Option 2, configure the driver to automatically refresh the access token on the headless machine.
Option 1: Obtaining and Exchanging a Verifier Code
To obtain a verifier code, you must authenticate at the OAuth authorization URL. Do the following:
-
Authenticate from the machine with an internet browser, and obtain the OAuthVerifier connection property.
If you are using the embedded OAuth application, call the GetOAuthAuthorizationURL stored procedure. Open the URL returned by the stored procedure in a browser.
If you are using a custom OAuth application, set the following properties:
- InitiateOAuth = OFF.
- OAuthClientId = the client Id assigned when you registered your application.
- OAuthClientSecret = the client secret assigned when you registered your application.
-
Call the GetOAuthAuthorizationURL stored procedure. The stored procedure returns the CallbackURL established when the custom OAuth application was registered. (See Creating a Custom OAuth Application.)
Copy this URL and paste it into a new browser tab.
-
Log in and grant permissions to the connector. The OAuth application redirects you the redirect URI, with a parameter called code appended. Note the value of this parameter; you will need it later, to configure the OAuthVerifier connection property.
-
Exchange the OAuth verifier code for OAuth refresh and access tokens. On the headless machine, set the following connection properties to obtain the OAuth authentication values:
- AccountId = the account to connect to.
- InitiateOAuth = REFRESH.
- OAuthVerifier = the noted verifier code (the value of the code parameter in the redirect URI).
- OAuthSettingsLocation = persist the encrypted OAuth authentication values to the specified file.
- Custom OAuth applications only:
- OAuthClientId = the client Id in your custom OAuth application settings.
- OAuthClientSecret = the client secret in the custom OAuth application settings.
-
Test the connection to generate the OAuth settings file.
-
After you re-set the following properties, you are ready to connect:
- AccountId = the account to connect to.
- InitiateOAuth = REFRESH.
- OAuthSettingsLocation = the file containing the encrypted OAuth authentication values. To enable the automatic refreshing of the access token, be sure that this file gives read and write permissions to the connector.
- Custom OAuth applications only:
- OAuthClientId = the client Id assigned when you registered your application.
- OAuthClientSecret = the client secret assigned when you registered your application.
Option 2: Transferring OAuth Settings
Prior to connecting on a headless machine, you must install and create a connection with the driver on a device that supports an internet browser. Set the connection properties as described above in "Desktop Applications".
After completing the instructions in "Desktop Applications", the resulting authentication values are encrypted and written to the path specified by OAuthSettingsLocation. The default filename is OAuthSettings.txt.
Test the connection to generate the OAuth settings file, then copy the OAuth settings file to your headless machine.
To connect to data via the headless machine, set the following connection properties:
- AccountId = the account to connect to.
- InitiateOAuth = REFRESH
- OAuthSettingsLocation = the path to the OAuth settings file you copied from the machine with the browser. To enable automatic refreshing of the access token, ensure that this file gives read and write permissions to the connector.
- Custom OAuth applications only:
- OAuthClientId = the client Id assigned when you registered your custom OAuth application.
- OAuthClientSecret = the client secret assigned when you registered your custom OAuth application.
Concurrent Requests
NetSuite allows only a certain number of concurrent requests per account, which is configurable per integration connection. (The default is usually 5.) If another request is made when the maximum number of concurrent requests is already in use, the user may receive an "Only one request may be made against a session at a time" error.The CData Python Connector for NetSuite attempts to account for this situation by staggering additional requests so as not to exceed the concurrent request limit. However, if an account is being connected to from multiple machines or applications, it is not always possible to do this correctly.