简体   繁体   English

DropNet DropBox登录,如何在控制台应用程序中以编程方式执行?

[英]DropNet DropBox login, how to do it programmatically in a console application?

Question: 题:

I'm using a DropBox csharp API from here: 我在这里使用DropBox csharp API:
https://github.com/dkarzon/DropNet https://github.com/dkarzon/DropNet

From the Unit tests, and the only working sample from here https://github.com/dkarzon/DropNet/blob/master/DropNet.Samples/DropNet.Samples.Web/Default.aspx.cs 从单元测试,以及这里唯一的工作样本https://github.com/dkarzon/DropNet/blob/master/DropNet.Samples/DropNet.Samples.Web/Default.aspx.cs

I figured that it works like this: 我认为它的工作原理如下:

DropNet.DropNetClient client = new DropNet.DropNetClient(strApiKey, strAppSecret);

DropNet.Models.UserLogin login = client.GetToken();
client.UserLogin = login;

var accountInfo = client.AccountInfo();
str = accountInfo.quota_info.quota.ToString();

The probem is, it throws an exception on accountinfo. 探测器是,它会在accountinfo上引发异常。 (System.Net.HttpStatusCode.Unauthorized) Everything before works fine, I get the login (usertoken & usersecret) . (System.Net.HttpStatusCode.Unauthorized)一切正常,我得到登录(usertoken&usersecret)。

I think my problem is this part of the sample application: 我认为我的问题是示例应用程序的这一部分:

var url = _client.BuildAuthorizeUrl(Request.Url.ToString() + "?dropboxcallback=1");
Response.Redirect(url);

Where it redirects to dropbox for a login... I don't have a web application, so I have no URL... 它重定向到Dropbox进行登录...我没有Web应用程序,所以我没有URL ...

What I have is a console application, that should make a backup of my database every evening automatically as a service, for which it certainly is very bad requiring a webbrowser and a user which has to type in email/username + password. 我所拥有的是一个控制台应用程序,应该每天晚上自动备份我的数据库作为服务,对于它来说当然非常糟糕,需要webbrowser和用户必须输入电子邮件/用户名+密码。

How can I do a login by directly supplying the hardcoded username and password ? 如何通过直接提供硬编码的用户名和密码来登录?

If I use the sample application, then it works, but that requires typing in the username and password on the web, and that sucks big time for a console application... 如果我使用示例应用程序,那么它可以工作,但这需要在Web上输入用户名和密码,这对于控制台应用程序来说是很大的时间......

As far as I know from other API's (facebook, google, stack exchange etc.) you'll have to redirect your user to a webpage of Dropbox, where it will grant permissions to you to use it's account to perform things. 据我所知,从其他API(Facebook,谷歌,堆栈交换等),您将不得不将您的用户重定向到Dropbox的网页,在那里它将授予您使用它的帐户执行操作的权限。

So in general it is not possible to achive this without a webbrower . 所以一般来说,没有webbrower就不可能实现这一目标 Otherwise you'll have to perform really dirty hacks to hack arround the permission system of dropbox. 否则你将不得不执行非常脏的黑客攻击Dropbox的权限系统。

Please have a look at "OAuth 2.0 authorization flow" on google. 请在google上查看“OAuth 2.0授权流程”。 Here's a diagram I found at Yahoo which show's how it works: 这是我在雅虎发现的一个图表,它显示了它的工作原理:

OAuth 2.0流程

对于Unetng DropnetClient的4参数构造函数,我们还需要构建基于web的url并允许用户验证他的帐户这是一个compusory的东西,在用户点击认证过程中的allow按钮后会生成accesstoken

As GameScripting explained the Dropbox API uses oauth which requires user login through the dropbox website to authenticate the access tokens. 正如GameScripting所解释的,Dropbox API使用oauth,要求用户通过Dropbox网站登录来验证访问令牌。

Checkout the documentation here: http://dkdevelopment.net/what-im-doing/dropnet/ for the 3 step process. 在这里查看文档: http//dkdevelopment.net/what-im-doing/dropnet/进行3步骤。

What sort of application are you building? 你在建造什么样的应用程序? Normal process is to load a browser control inside the application and navigate to the login URL with it. 正常过程是在应用程序内加载浏览器控件并使用它导航到登录URL。

Also have a look at the sample Windows Phone app to give you an idea of how this process works: https://github.com/dkarzon/DropNet/blob/master/DropNet.Samples/DropNet.Samples.WP7/MainPage.xaml.cs 另请查看示例Windows Phone应用程序,以了解此过程的工作原理: https//github.com/dkarzon/DropNet/blob/master/DropNet.Samples/DropNet.Samples.WP7/MainPage.xaml的.cs

Instead of hardcoding the username and password, you can hardcode the OAuth access token. 您可以对OAuth访问令牌进行硬编码,而不是对用户名和密码进行硬编码。

First, create a simple program (using the same app key) that follows the standard browser-based authorization flow. 首先,创建一个遵循标准的基于浏览器的授权流程的简单程序(使用相同的应用程序密钥)。 Then use it to authorize the app with the desired user. 然后使用它来为所需用户授权应用程序。 This will give you an OAuth access token (a "token" and "token secret") associated with that user and your app key. 这将为您提供与该用户和您的应用密钥相关联的OAuth访问令牌(“令牌”和“令牌密钥”)。

Then, in your service-style application, just hardcode the OAuth access token (using DropNetClient 's 4-argument constructor). 然后,在服务式应用程序中,只需对OAuth访问令牌进行硬编码(使用DropNetClient的4参数构造函数)。

It is possible, using SharpBox - tested, works. 使用SharpBox进行测试是可行的。
One needs to acquire the AccessToken as a one-time-action manually, then after that, one can omit the login page and use the saved AccessToken. 需要手动获取AccessToken作为一次性操作,然后可以省略登录页面并使用保存的AccessToken。

http://www.jayway.com/2012/02/06/unboxing-dropbox-and-sharpbox-2/ http://www.jayway.com/2012/02/06/unboxing-dropbox-and-sharpbox-2/

The magic line is: 神奇的线条是:

Globals.DropBox.Token = AppLimit.CloudComputing.SharpBox.StorageProvider.DropBox.DropBoxStorageProviderTools
.ExchangeDropBoxRequestTokenIntoAccessToken(
      Globals.DropBox.config
    , Globals.DropBox.AppKey, Globals.DropBox.AppSec
    , Globals.DropBox.requestToken
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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