tnblog
首页
视频
资源
登录
愿你出走半生,归来仍是少年
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术

c#获取百度ai平台的access_token

5433人阅读 2020/2/12 19:13 总访问:1635492 评论:0 收藏:0 手机
分类: NET

代码如下:

using System;
using System.Collections.Generic;
using System.Net.Http;

namespace com.baidu.ai
{
	public static class AccessToken

	{
	    // 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
              // 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
		private static String clientId = "你的API Key";
		// 百度云中开通对应服务应用的 Secret Key
		private static String clientSecret = "你的Secret Key";

		public static String getAccessToken() {
			String authHost = "https://aip.baidubce.com/oauth/2.0/token";
			HttpClient client = new HttpClient();
			List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
			paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
			paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
			paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));

			HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
			String result = response.Content.ReadAsStringAsync().Result;
			Console.WriteLine(result);
			return result;
		}
	}
}	


欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739

评价