从 asp.net 应用程序的框 API 获取访问令牌?
Get an access token from box API from asp.net application?
我有我的授权码、客户 ID、客户密码,现在要将文件上传到我的 box 帐户,我需要有访问令牌。我正在使用下面复制在 Whosebug 某处的代码来获取访问令牌。
public string GetAccessToken(string code, string ClientId, string ClientSecret)
{
RestClient rs = new RestClient();
string grant_type = "authorization_code";
RestRequest request = new RestRequest(Method.POST);
IRestRequest reuest = request;
string strHeaders = null;
RestResponse response = default(RestResponse);
IRestResponse resp = response;
string strResponse = null;
try
{
rs.BaseUrl = "https://www.box.com/api/oauth2/token";
request.Resource = "oauth2/token";
strHeaders = string.Format("grant_type={0}&code={1}&client_id={2}&client_secret={3}", grant_type, code, ClientId, ClientSecret);
request.AddHeader("Authorization", strHeaders);
resp = rs.Execute(reuest);
strResponse = resp.Content;
return strResponse;
}
catch (Exception ex)
{
throw ex;
}
}
响应的内容类型是 HTML,而不是他们在文档页面中提到的 JSON。能否请您帮助我如何使用 asp.net 应用程序从 BOX API 获取访问令牌?
下面的代码完成了工作:
public string GetAccessToken()
{
string param = string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}", CODE, CLIENT_ID, CLIENT_SECRET);
var client = new RestClient("https://api.box.com/oauth2/token/");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", param, ParameterType.RequestBody);
var response = client.Execute(request);
var json = JObject.Parse(response.Content);
return Convert.ToString(json["access_token"]);
}
请注意我在代码中添加的 Header 和参数。
我有我的授权码、客户 ID、客户密码,现在要将文件上传到我的 box 帐户,我需要有访问令牌。我正在使用下面复制在 Whosebug 某处的代码来获取访问令牌。
public string GetAccessToken(string code, string ClientId, string ClientSecret)
{
RestClient rs = new RestClient();
string grant_type = "authorization_code";
RestRequest request = new RestRequest(Method.POST);
IRestRequest reuest = request;
string strHeaders = null;
RestResponse response = default(RestResponse);
IRestResponse resp = response;
string strResponse = null;
try
{
rs.BaseUrl = "https://www.box.com/api/oauth2/token";
request.Resource = "oauth2/token";
strHeaders = string.Format("grant_type={0}&code={1}&client_id={2}&client_secret={3}", grant_type, code, ClientId, ClientSecret);
request.AddHeader("Authorization", strHeaders);
resp = rs.Execute(reuest);
strResponse = resp.Content;
return strResponse;
}
catch (Exception ex)
{
throw ex;
}
}
响应的内容类型是 HTML,而不是他们在文档页面中提到的 JSON。能否请您帮助我如何使用 asp.net 应用程序从 BOX API 获取访问令牌?
下面的代码完成了工作:
public string GetAccessToken()
{
string param = string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}", CODE, CLIENT_ID, CLIENT_SECRET);
var client = new RestClient("https://api.box.com/oauth2/token/");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", param, ParameterType.RequestBody);
var response = client.Execute(request);
var json = JObject.Parse(response.Content);
return Convert.ToString(json["access_token"]);
}
请注意我在代码中添加的 Header 和参数。