TI store API 套件

身份验证

由于 TI store API 使用 OAuth 2.0 进行保护,因此您必须在发送请求时于标头中传递访问令牌。要获取访问令牌,请调用 OAuth API,网址为 https://transact.ti.com/v1/oauth/accesstoken

要使请求取得成功,请注意:
 

  • 我们使用客户端凭据流。
  • “Content-Type”必须是“application/x-www-form-urlencoded”。
  • 应将请求发送至上面的相应 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" }


来自 Insomnia 客户端的访问令牌请求的示例身份验证有效载荷和标头:

 


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<string, string>("grant_type","client_credentials"), new KeyValuePair<string, string>("client_id","{myTI API Key}"), new KeyValuePair<string, string>("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.响应:" + responseBody); } else { Console.WriteLine("Request failed.状态代码:" + response.StatusCode); } } catch (Exception e) { Console.WriteLine(e.Message); } // Process the response } } } } }