123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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 = "<HTML><BODY> Hello world!</BODY></HTML>";
- 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;
- }
- /// <summary>
- /// Handles the redirect from Dropbox server. Because we are using token flow, the local
- /// http server cannot directly receive the URL fragment. We need to return a HTML page with
- /// inline JS which can send URL fragment to local server as URL parameter.
- /// </summary>
- /// <param name="http">The http listener.</param>
- /// <returns>The <see cref="Task"/></returns>
- private async Task HandleOAuth2Redirect(HttpListener http)
- {
- var context = await http.GetContextAsync();
- // We only care about request to RedirectUri endpoint.
- while (context.Request.Url.AbsolutePath != RedirectUri.AbsolutePath)
- {
- context = await http.GetContextAsync();
- }
- context.Response.ContentType = "text/html";
- // Respond with a page which runs JS and sends URL fragment as query string
- // to TokenRedirectUri.
- using (var file = System.IO.File.OpenRead("index.html"))
- {
- file.CopyTo(context.Response.OutputStream);
- }
- context.Response.OutputStream.Close();
- }
- /// <summary>
- /// Handle the redirect from JS and process raw redirect URI with fragment to
- /// complete the authorization flow.
- /// </summary>
- /// <param name="http">The http listener.</param>
- /// <returns>The <see cref="OAuth2Response"/></returns>
- private async Task<Uri> HandleJSRedirect(HttpListener http)
- {
- var context = await http.GetContextAsync();
- // We only care about request to TokenRedirectUri endpoint.
- while (context.Request.Url.AbsolutePath != JSRedirectUri.AbsolutePath)
- {
- context = await http.GetContextAsync();
- }
- var redirectUri = new Uri(context.Request.QueryString["url_with_fragment"]);
- return redirectUri;
- }
- }
- }
|