2013-07-05 266 views
1

我必須上傳文件到box.com因爲我需要授權並獲取accesstoken和刷新令牌。我沒有在c#asp.net中找到任何代碼。我希望使用c#和asp.net進行身份驗證的代碼以及獲取accesstoken和刷新令牌的代碼。我在下面的代碼嘗試獲取錯誤,因爲頁面已過期請求再次。 這裏是C#asp.net.I代碼使用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; 
     } 
    } 

回答

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

+0

非常感謝期待你的答覆 – user2514945

0

您還需要設置編碼:

request.RequestFormat = DataFormat.Xml;