TI store API スイート

認証

TI store API は OAuth 2.0 を使用し、セキュアなアクセスを実現します。これらの API を操作するには、リクエスト ヘッダーに含まれているアクセス トークンが必要です。アクセス トークンを取得するには、https://transact.ti.com/v1/oauth/accesstoken で OAuth API を呼び出します。

アクセストークンの取得

トークンを取得するために、クライアント資格情報フローを使用します。リクエストを成功させるには、次の点を確認します。

  • コンテンツの種類:「application/x-www-form-urlencoded」に設定します
  • リクエスト URL:追加のクエリ パラメータなしで、提供された URL を使用します。
  • リクエスト本文:以下のパラメータを含めます (エンコードなしで「&」で区切ってください)。
    • grant_type
    • client_id
    • client_secret

リクエスト本文の例:

"grant_type=client_credentials&client_id=[CLIENT_ID]&client_secret=[CLIENT_SECRET]"

アクセス トークンの使用

  • トークンの有効な期間は 60 分です。他の API でトークンを使用する前に、このアクセス トークンが期限切れになっているかどうかを確認してください。
  • すべての API 要求のヘッダーにアクセス トークン (ベアラー トークン) を含めます。

 

curl --request POST \   
       --url https://transact.ti.com/v1/oauth/accesstoken \   
       --header 'Content-Type: application/x-www-form-urlencoded' \   
       --data grant_type=client_credentials \   
       --data client_id=XXXXXXXXXXXXXXXXX \   
       --data client_secret=XXXXXXXXXXXXXXXXX


要求が成功した場合の応答:

{
    "access_token": "MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3",
    "token_type": "bearer",
    "expires_in": 3599,
    "scope": "",
    "application_name": "app_name",
    "developer.email": "api-portal@list.ti.com",
    "issued_at": "1582220284531",
    "client_id": "IwOjYzmalmM2YxOT15MGE3YmNm4DFkyTVk"
}

 

Example authentication payload and header of an access token request from Insomnia client:


Insomnia 要求の OAuth2 設定:


VB.NET (Framework 4.6.1) での認証の例:

Imports System.Net
Imports System.IO
Imports System.Text
Imports Newtonsoft.Json.Linq

Private Function GetToken() as string

        Dim URL As String = "https://transact.ti.com/v1/oauth/accesstoken"

        Dim ClientID As String = "{myTI API Key}"
        Dim ClientSecret As String = "{myTI API Secret}"
        Dim Data As String = $"grant_type=client_credentials&client_id={ClientID }&client_secret={ClientSecret}"

        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"   
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(Data)
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim result As String = reader.ReadToEnd()
        reader.Close()
        dataStream.Close()
        response.Close()

        Dim parsejson As JObject = JObject.Parse(result)

        return parsejson.SelectToken("access_token").ToString

End Function

 

C# (framework 4.6.1) での認証の例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetToken();
        }

        static async Task GetToken()
        {
            string apiUrl = "https://transact.ti.com/v1/oauth/accesstoken";

            // Create the HttpClient
            using (HttpClient client = new HttpClient())
            {
                // Prepare the data to send
                var formData = new FormUrlEncodedContent(new[]
                {
                new KeyValuePair("grant_type", "client_credentials"),
                new KeyValuePair("client_id", "{myTI API Key}"),
                new KeyValuePair("client_secret", "{myTI API Secret}"),
                // Add more key-value pairs as needed
            });

                // Prepare the PUT request
                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, apiUrl))
                {
                    request.Content = formData;
                    try
                    {
                        request.Headers.Add("Accept", "application/json"); // Optional, set the desired response format}
                    }


                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }


                    // Send the request and get the response
                    try
                    {
                        HttpResponseMessage response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            string responseBody = await response.Content.ReadAsStringAsync();
                            Console.WriteLine("Request successful. Response: " + responseBody);
                        }
                        else
                        {
                            Console.WriteLine("Request failed. Status code: " + response.StatusCode);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    // Process the response

                }
            }
        }
    }
}