Part Number: TI-API
Hi TI supports,
I am using Google Apps Scripts to access the API. When I send the following code to TI-API, it always return "error": "invalid_client".
The client ID and SECRET are valid and can run in python and TI test website.
Not sure what happened, could you provide a JavaScript example or help me to dragonize the following codes?
Thank you very much!
--------------------codes-----------------
/** Documemtation for TI-OAuth: api-portal.ti.com/store-api-authentication
** Documentation for TI-Inv: api-portal.ti.com/store-inventory-pricing-api
** Sample Code that I use (Notion.gs): github.com/.../apps-script-oauth2
**/
var GRANT_TYPE = 'client_credentials'
var CLIENT_ID = 'xxxxxxxxxmaskedxxxxxxxxxx';
var CLIENT_SECRET = 'xxxxxxxxxmaskedxxxxxxxxxx';
/** * Authorizes and makes a request to the Texas API. */
function run() {
var service = getService_();
if (service.hasAccess()) {
var options = {
'methods':'post',
'contenttype' : 'application/x-www-form-urlencoded',
'muteHttpExceptions': true,
headers: {
Authorization: 'Bearer ' + service.getAccessToken(),
}}
var response = UrlFetchApp.fetch(url, options);
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
} else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}
/** * Reset the authorization state, so that it can be re-tested. */
function reset() {
getService_().reset();
}
/** * Configures the service. */
function getService_() {
return OAuth2.createService('TexasGetService')
// Set the endpoint URLs.
.setAuthorizationBaseUrl(
.setTokenUrl(
.setTokenHeaders({
'Authorization': 'Basic ' + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET)})
// Set the client ID and secret.
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
// Set the name of the callback function that should be invoked to
// complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
;
}
/**
* Handles the OAuth callback.
*/
function authCallback(request) {
var service = getService_();
var authorized = service.handleCallback(request);
if (authorized) {
return HtmlService.createHtmlOutput('Success!');
} else {
return HtmlService.createHtmlOutput('Denied.');
}
}
/**
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(OAuth2.getRedirectUri());
}