[英]how to use box api in asp.net web application
I am trying to use box api in an asp.net web application. 我正在尝试在asp.net Web应用程序中使用Box API。
Based on the search there are two options to access box account; 根据搜索,有两个访问邮箱帐户的选项:
By downloading the Box.V2 package using below link containing the required dlls and use that in our application 通过使用包含所需dll的以下链接下载Box.V2软件包,并在我们的应用程序中使用它
By using Box SDK containing code and reference that inside our application. 通过使用包含代码的Box SDK并在我们的应用程序内部进行引用。 Using this approach we can debug the Box.V2 code by adding the project to our solution. 使用这种方法,我们可以通过将项目添加到解决方案中来调试Box.V2代码。
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. 有人可以通过指定要采取的步骤,.net框架的最低要求等来帮助我前进。
Good question, GitHub samples does not mention about the Web (Asp.Net). 好问题,GitHub示例未提及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. 可能的话,一旦您找到方法,它看起来就很容易做到,我已经看到Windows应用程序尝试手动构建授权URL等的一些答案,但是有一种更简单的方法。
Here's how to do it with OAuth, 这是使用OAuth的方法,
PM> Install-Package Box.V2 PM>安装包Box.V2
Get the Authcode (this is what's been missing in most examples) 获取Authcode(这是大多数示例中所缺少的)
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. 有趣的是,“ config”对象生成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. 这会将用户重定向到“同意”屏幕,并要求用户登录。一旦用户“授予访问权限”,您将获得用于回叫URL的“ Authcode”,可用于生成访问令牌。
Handle the Auth Callback response 处理Auth回调响应
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--- }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.