You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
2.1 KiB
C#
54 lines
2.1 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Web;
|
|
|
|
namespace TradeManageNew
|
|
{
|
|
public static class UPSTokenHelp
|
|
{
|
|
const string client_id = "NeG6fE37MEZAjzzWebCyd1gM78jUa5kt741VJLmiiF0mKx5D";
|
|
const string client_secret = "f7jmbk077R94iTzW3gKRBePReNjqR6XtoHxZ5q10cvxmUrxxcG6m0Kdm61oEGhmY";
|
|
public static string GetUPSToken()
|
|
{
|
|
//var test_uri = "https://wwwcie.ups.com/security/v1/oauth/token";
|
|
var zs_uri = "https://onlinetools.ups.com/security/v1/oauth/token";
|
|
|
|
// 创建HttpClient实例
|
|
HttpClient client = new HttpClient();
|
|
|
|
// 设置Basic Auth验证
|
|
string auth = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{client_id}:{client_secret}"));
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);
|
|
|
|
// 添加自定义Header
|
|
client.DefaultRequestHeaders.Add("x-merchant-id", client_id);
|
|
|
|
// 添加自定义Body
|
|
var requestBody = new Dictionary<string, object>
|
|
{
|
|
{"grant_type","client_credentials"}
|
|
};
|
|
var bodyStr = JsonConvert.SerializeObject(requestBody);
|
|
|
|
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
|
|
// 创建 HttpRequestMessage 实例
|
|
var request = new HttpRequestMessage(HttpMethod.Post, zs_uri);
|
|
request.Content = new StringContent("grant_type=client_credentials", System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
|
|
|
|
// 发送 POST 请求并返回响应结果
|
|
var response = client.SendAsync(request).Result;
|
|
var result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
|
|
|
var resObj = JsonConvert.DeserializeObject<UPSTokenModel>(result);
|
|
Console.WriteLine(result);
|
|
// Console.ReadKey();
|
|
return resObj.access_token;
|
|
}
|
|
}
|
|
} |