c# HttpClient 加header Authorization 认证

 paypal c#accesstoken获取

public static string GenerateAccessToken()
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            string PAYPAL_CLIENT_ID = "";
            string PAYPAL_CLIENT_SECRET = "";
            var client = new HttpClient();

            client.BaseAddress = new Uri("https://api-m.sandbox.paypal.com/");
            var authValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(PAYPAL_CLIENT_ID + ":" + PAYPAL_CLIENT_SECRET));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "QVZiQ0lmdlFKV0lJZlluZ2hrTERQblJxUkV6dFFMWnh5a2M5am1GNVlIRzZTU051YUNENERzd09CYlBab2JTUzMxb21MOTRGMlRITEdSc0g6RUp2WnR2cW1ieVVfdlJUTHFKdk5ycjNSb1p0akY4OVBVUVlDd0JOOHM3NWNkckpucjhoRXZUTlVhclhYcmhENnZTeHN4dVJRSmxJcGFoako=");

            var response = client.PostAsync("v1/oauth2/token",

                new StringContent("grant_type=client_credentials", System.Text.Encoding.UTF8, "application/x-www-form-urlencoded")
            ).Result;

            var responseData = response.Content.ReadAsStringAsync().Result;
            dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(responseData);
            //LogHelper.WriteLog(json.access_token);
            return json.access_token;

        }

.net 4.5 需要加上https协议认真

ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

c# webapi项目,异步请求必需同步接收,要不就会接收不到,异步方法不受影响

var responseData = response.Content.ReadAsStringAsync().Result;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值