我有我的身份验证码,客户端ID,客户端密码,现在要将文件上传到我的盒子帐户,我需要获取ACCESS TOKEN。我正在使用下面的代码复制到stackoverflow中的某处以获取ACCESS TOKEN。
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获取访问令牌吗?
答案 0 :(得分:2)
下面的代码完成了工作:
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"]);
}
请注意我在代码中添加的标题和参数。