简体   繁体   English

如何使用C#获取本地Dropbox文件的共享URL

[英]How do I get the share url of a local dropbox file using C#

I am using the DropNet API to retrieve the share link of a local file but cannot get past the GetAccessToken() because it is null. 我正在使用DropNet API来检索本地文件的共享链接,但无法通过GetAccessToken(),因为它为null。

Here is my code so far: 到目前为止,这是我的代码:

var _client = new DropNetClient(APIKEY, APISEC);
_client.GetToken();
var url = _client.BuildAuthorizeUrl();
Process.Start(url);
var accessToken = _client.GetAccessToken();                    
var shareResponse = _client.GetShare("/Getting Started.rtf");
MessageBox.Show(shareResponse.Url);

exception thrown at GetShare(): 抛出GetShare()的异常:

System.Reflection.TargetInvocationException

It has to be something with how I am launching the browser but I've been at this for hours, thanks for any help! 这与我启动浏览器的方式有关,但是我已经花了几个小时了,谢谢您的帮助!

After hours and hours I finally found decent documentation from Spring.Social (from NuGet). 几个小时之后,我终于从Spring.Social(来自NuGet)找到了不错的文档。

Here it is: 这里是:

  1. Sign up for a new app to link to your program here: DropBox / Developer / Apps 在此处注册新应用以链接到您的程序: DropBox / Developer / Apps

  2. Install the NuGet Package Spring.Social.Dropbox. 安装NuGet包Spring.Social.Dropbox。

  3. Use the example below to connect. 使用下面的示例进行连接。

     using Spring.Social.Dropbox.Api; using Spring.Social.Dropbox.Connect; using Spring.Social.OAuth1; private void ShareViaDropbox(string[,] filesNameLink) { try { if (CheckForInternetConnection()) // cant use dropbox without internet. { string consumerKey = "<get this from dropbox>"; string consumerSecret = "<get this from dropbox>"; DropboxServiceProvider dropboxServiceProvider = new DropboxServiceProvider(consumerKey, consumerSecret, AccessLevel.Full); IOAuth1Operations oauthOperations = dropboxServiceProvider.OAuthOperations; // I used an xml file to store the key (probably not the most secure later I will write an encryption). string[] storedToken = GetTag("<DropBoxToken>", ",").Split(','); OAuthToken oauthAccessToken = new OAuthToken(storedToken[0], storedToken[1]); // testing if we got something from the earlier xml tag. if (oauthAccessToken.Value == string.Empty || oauthAccessToken.Value == "") { Console.Write("Getting request token..."); OAuthToken oauthToken = dropboxServiceProvider.OAuthOperations.FetchRequestTokenAsync(null, null).Result; Console.WriteLine("Done"); OAuth1Parameters parameters = new OAuth1Parameters(); string authenticateUrl = dropboxServiceProvider.OAuthOperations.BuildAuthorizeUrl(oauthToken.Value, parameters); Console.WriteLine("Redirect user for authorization"); // let the user know they have to sign in and chose the correct dropbox. this is also where most documentation leaves off! MessageBox.Show( "This is the first time you have connected to DropBox. A browser will open and direct you the the DropBox authentication page. When you prompted, please sign in and then choose the [named] DropBox." + Environment.NewLine + "Once you have succesfully compleaded the authorization confirmation from DropBox, you may close the browser to continue." + Environment.NewLine + Environment.NewLine + "Click 'Ok' to proceed", "DropBox Authentication Required.", MessageBoxButtons.OK, MessageBoxIcon.Information); Process.Start(authenticateUrl); // halt the program until the user has authenticated. MessageBox.Show("Click 'OK' when authorization has succeeded.", "DropBox Authentication - WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop); Console.Write("Press any key when authorization attempt has succeeded"); Console.Write("Getting access token..."); AuthorizedRequestToken requestToken = new AuthorizedRequestToken(oauthToken, null); oauthAccessToken = dropboxServiceProvider.OAuthOperations.ExchangeForAccessTokenAsync(requestToken, null).Result; Console.WriteLine("Done"); ChangeTag("<DropBoxToken>", oauthAccessToken.Value + "," + oauthAccessToken.Secret); // update the xml file with the token. } IDropbox dropbox = dropboxServiceProvider.GetApi(oauthAccessToken.Value, oauthAccessToken.Secret); DropboxLink shareableLink; double filesNameLength = Math.Ceiling(Convert.ToDouble(filesNameLink.Length / 2)); // remove everything before the users special folder path including the dropbbox folder. // then change all the switches to front from back for the web. for (int i = 0; i < filesNameLength; i++) { filesNameLink[i, 1] = filesNameLink[i, 0]; filesNameLink[i, 0] = Path.GetFileName(filesNameLink[i, 0]); filesNameLink[i, 1] = filesNameLink[i, 1].Replace(dropboxRootDir, "").Replace("\\\\", "/"); shareableLink = dropbox.GetShareableLinkAsync(filesNameLink[i, 1].Replace("\\\\", "/")).Result; filesNameLink[i, 1] = shareableLink.Url; } // do something with the link(s). // catch { } } } catch { } } 

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

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