简体   繁体   中英

how to use box api in asp.net web application

I am trying to use box api in an asp.net web application.

Based on the search there are two options to access box account;

  1. By downloading the Box.V2 package using below link containing the required dlls and use that in our application

  2. By using Box SDK containing code and reference that inside our application. Using this approach we can debug the Box.V2 code by adding the project to our solution.

Correct me if I am wrong.

So, I am trying to implement the second approach. Can someone help me move forward by specifying the steps to be taken, minimum .net framework requirement, etc.

Good question, GitHub samples does not mention about the Web (Asp.Net). It's possible and it looks pretty easy to do once you figure out the the way, I have seen some answers for Windows apps trying to manually build the authorization URLs etc, but there is an easier way to do it.

Here's how to do it with OAuth,

  1. Install nuget

PM> Install-Package Box.V2

  1. Get the Authcode (this is what's been missing in most examples)

     public async Task<ActionResult> Connect() { var clientId = "xxxxx"; var clientSecret = "xxxxxx"; var redirectUri = new Uri("http://localhost:xxxx/Home/AuthCallBackAsync");//Your call back URL var config = new BoxConfig(clientId, clientSecret, redirectUri); return Redirect(config.AuthCodeUri.ToString()); } 

Interesting thing is that the "config" object generates the AuthCodeUri. This will redirect the user to Consent screen and ask the user to sign in. Once the user "Grants Access" you will get the "Authcode" for your call back URL which can be used to generate accesstoken.

  1. Handle the Auth Callback response

     public async Task<ActionResult> AuthCallbackAsync() { NameValueCollection parms = Request.QueryString; var authCode = parms["code"] //Get "config" - you can store this in session or in a cache. var config = new BoxConfig(clientId, clientSecret, redirectUri); var client = new BoxClient(config); await client.Auth.AuthenticateAsync(authCode); //Now you will get the accesstoken and refresh token var accessToken = client.Auth.Session.AccessToken; var refreshToken = client.Auth.Session.RefreshToken; //Ready to consume the API var user = await client.UsersManager.GetCurrentUserInformationAsync(); -------More Api Calls--- } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM