1

我必须将文件上传到box.com,因为我需要授权并获取accesstoken和刷新令牌。我在 c# asp.net 中找不到任何代码。我想要使用 c# 和 asp.net 进行身份验证的代码以及用于获取accesstoken和刷新令牌的代码。我尝试了下面的代码,因为页面再次过期请求而出现错误。这是 c# asp.net 中的代码。我正在尝试使用Restsharp

public void 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;

            Label1.Text = strResponse;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
4

2 回答 2

1

来自文档: https ://developers.box.com/oauth/ (请参阅“获取访问令牌”)

在为一组访问令牌和刷新令牌交换身份验证代码时,您需要向https://www.box.com/api/oauth2/token端点发出 POST 请求。

尝试将您在标题的“授权”部分中添加的内容放入 URL 编码的 POST 正文中。

或者更好的是,尝试可用的 .NET SDK,它将为您处理 OAuth 工作流的这一部分: https ://github.com/box/box-windows-sdk-v2

于 2013-07-15T17:18:26.597 回答
0

您还需要设置编码:

request.RequestFormat = DataFormat.Xml;
于 2013-08-21T21:46:58.653 回答