HI TI support,
I was trying to use for_in_loop to search inventory for a list of components in my google sheet.
That is:
data = result.get('values', [ ])
for CPlist in data:
....
and it retuned that I am using the first available part-number in google sheet to search, which is ['OPA2348AIDCNT']
When debugging, line 63 returns a warning: Unexpected HTTP status code 404.
However, same code in line 63 works pefectly with assigned part number 'OPA2348AIDCNT'
I assume that is because the extra Brackets and Apostrophes " [' '] " caused this problem but I don't know how to fix it since the value is from another (Google sheet) API.
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.oauth2 import service_account
from TI_API_Suite import TI_Orders, TI_Inventory
from API_Accessor import API_Accessor
# GOOGLE SHEET: If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = '/Users/MingC_MBP/Desktop/工作/API projects/GoogleAPI/dkaotest1key.json'
creds = None
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
# GOOGLE SHEET: The ID of Google Sheet.
DOCUMENT_ID = '---masked---'
service = build('sheets', 'v4', credentials=creds)
# GOOGLE SHEET: --- Call the Sheets API ; get values in google sheet
# values = result.get('values', [])
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=DOCUMENT_ID,
range="Components RT-View!K2:K").execute()
#print(result)
data = result.get('values',[])
for CPlist in data:
# TI API: --- use google sheet result as entry to TI API and search for Inv ---
# TI API: Store user credentials and server URL address.
client_id = "---masked---"
client_secret = "masked---"
server = "https://transact.ti.com"
verify_ssl = True
# TI API: Confirm to user.
print("\nLogging into {} with ID {}.".format(server, client_id))
api = API_Accessor(server=server, client_id=client_id, client_secret=client_secret, verify=verify_ssl)
# TI API: Print output.
print("Access token {} retrieved!".format(api.token))
print("Expires on {}.".format(api.expiration.strftime("%m/%d/%Y at %H:%M:%S")))
# TI API: Confirm to user.
print("\nLogging into Inventory & Pricing API {} with ID {} and secret {}.".format(server, client_id, client_secret))
catalog = TI_Inventory(server=server, client_id=client_id, client_secret=client_secret, verify=verify_ssl)
# TI API: Step 1: Get inventory of the OPN below.
opn = CPlist
print("\nFirst, we are using the inventory API to get how many items of OPN {} there are.".format(opn))
response = catalog.get_product_info(opn=opn, verify=verify_ssl)
# TI API: Step 2: View server response.
print("Response: There are {} of this part available.".format(response.json()['quantity']))