using Dropbox.Api; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PDF_Office.ViewModels.HomePanel.CloudDrive { public class DropbBoxUserItem { DropboxClient client; // This loopback host is for demo purpose. If this port is not // available on your machine you need to update this URL with an unused port. public static readonly string LoopbackHost = "http://127.0.0.1:8080/"; // Add an ApiKey (from https://www.dropbox.com/developers/apps) here public static readonly string ApiKey = "k1hv3601odbsmln"; // URL to receive OAuth 2 redirect from Dropbox server. // You also need to register this redirect URL on https://www.dropbox.com/developers/apps. public readonly Uri RedirectUri = new Uri(LoopbackHost + "authorize"); // URL to receive access token from JS. public readonly Uri JSRedirectUri = new Uri(LoopbackHost + "token"); private string currentFolder = ""; HttpListener http = new HttpListener(); private async void btnConnect_Click() { DropboxCertHelper.InitializeCertPinning(); var state = Guid.NewGuid().ToString("N"); var OAuthflow = new PKCEOAuthFlow(); var authorizeUri = OAuthflow.GetAuthorizeUri(OAuthResponseType.Code, ApiKey, RedirectUri.ToString(), state: state, tokenAccessType: TokenAccessType.Offline, scopeList: null, includeGrantedScopes: IncludeGrantedScopes.None); //var http = new HttpListener(); http.Prefixes.Clear(); http.Prefixes.Add(LoopbackHost); http.Start(); // IAsyncResult result = http.BeginGetContext(new AsyncCallback(ListenerCallback), http); System.Diagnostics.Process.Start(authorizeUri.ToString()); await HandleOAuth2Redirect(http); // Handle redirect from JS and process OAuth response. var redirectUri = await HandleJSRedirect(http); var tokenResult = await OAuthflow.ProcessCodeFlowAsync(redirectUri, ApiKey, RedirectUri.ToString(), state); // Window.GetWindow(this).Activate(); DropboxClientConfig dropboxClientConfig = new DropboxClientConfig("ControlTest"); client = new DropboxClient(tokenResult.AccessToken, dropboxClientConfig); RefreshList(); } public static void ListenerCallback(IAsyncResult result) { HttpListener listener = (HttpListener)result.AsyncState; // Call EndGetContext to complete the asynchronous operation. HttpListenerContext context = listener.EndGetContext(result); HttpListenerRequest request = context.Request; // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. string responseString = "
Hello world!"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); // You must close the output stream. output.Close(); } public async void RefreshList(string path = "") { var files = await client.Files.ListFolderAsync(path); // ItemList.ItemsSource = files.Entries; } ///