mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 09:17:36 +13:00
Code refactoring
This commit is contained in:
parent
59b30f7062
commit
63a5d8dfd3
2 changed files with 68 additions and 66 deletions
|
@ -1086,15 +1086,25 @@ private bool OAuth2Complete(IOAuth2Basic uploader, string code, OAuthControl con
|
||||||
|
|
||||||
private async Task<OAuth2Info> OAuth2Loopback(IOAuth2Loopback oauth)
|
private async Task<OAuth2Info> OAuth2Loopback(IOAuth2Loopback oauth)
|
||||||
{
|
{
|
||||||
OAuthListener listener = new OAuthListener(oauth);
|
try
|
||||||
bool result = await listener.ConnectAsync();
|
|
||||||
|
|
||||||
this.ForceActivate();
|
|
||||||
ConfigureOAuthStatus(oauth2YouTube, result);
|
|
||||||
|
|
||||||
if (result)
|
|
||||||
{
|
{
|
||||||
return listener.OAuth.AuthInfo;
|
using (OAuthListener listener = new OAuthListener(oauth))
|
||||||
|
{
|
||||||
|
bool result = await listener.ConnectAsync();
|
||||||
|
|
||||||
|
this.ForceActivate();
|
||||||
|
ConfigureOAuthStatus(oauth2YouTube, result);
|
||||||
|
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
return listener.OAuth.AuthInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
DebugHelper.WriteException(e);
|
||||||
|
e.ShowError();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -53,77 +53,69 @@ public void Dispose()
|
||||||
|
|
||||||
public async Task<bool> ConnectAsync()
|
public async Task<bool> ConnectAsync()
|
||||||
{
|
{
|
||||||
|
Dispose();
|
||||||
|
Code = null;
|
||||||
|
|
||||||
|
IPAddress ip = IPAddress.Loopback;
|
||||||
|
int port = URLHelpers.GetRandomUnusedPort();
|
||||||
|
string redirectURI = string.Format($"http://{ip}:{port}/");
|
||||||
|
|
||||||
|
OAuth.RedirectURI = redirectURI;
|
||||||
|
string url = OAuth.GetAuthorizationURL();
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(url))
|
||||||
|
{
|
||||||
|
URLHelpers.OpenURL(url);
|
||||||
|
DebugHelper.WriteLine("Authorization URL is opened: " + url);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DebugHelper.WriteLine("Authorization URL is empty.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Dispose();
|
listener = new HttpListener();
|
||||||
Code = null;
|
listener.Prefixes.Add(redirectURI);
|
||||||
|
listener.Start();
|
||||||
|
|
||||||
IPAddress ip = IPAddress.Loopback;
|
HttpListenerContext context = await listener.GetContextAsync();
|
||||||
int port = URLHelpers.GetRandomUnusedPort();
|
Code = context.Request.QueryString.Get("code");
|
||||||
string redirectURI = string.Format($"http://{ip}:{port}/");
|
|
||||||
|
|
||||||
OAuth.RedirectURI = redirectURI;
|
using (HttpListenerResponse response = context.Response)
|
||||||
string url = OAuth.GetAuthorizationURL();
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(url))
|
|
||||||
{
|
{
|
||||||
URLHelpers.OpenURL(url);
|
string status;
|
||||||
DebugHelper.WriteLine("Authorization URL is opened: " + url);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
DebugHelper.WriteLine("Authorization URL is empty.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
if (!string.IsNullOrEmpty(Code))
|
||||||
{
|
|
||||||
listener = new HttpListener();
|
|
||||||
listener.Prefixes.Add(redirectURI);
|
|
||||||
listener.Start();
|
|
||||||
|
|
||||||
HttpListenerContext context = await listener.GetContextAsync();
|
|
||||||
Code = context.Request.QueryString.Get("code");
|
|
||||||
|
|
||||||
using (HttpListenerResponse response = context.Response)
|
|
||||||
{
|
{
|
||||||
string status;
|
status = "Authorization completed successfully.";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
status = "Authorization did not succeed.";
|
||||||
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(Code))
|
string responseText = Resources.OAuthCallbackPage.Replace("{0}", status);
|
||||||
{
|
byte[] buffer = Encoding.UTF8.GetBytes(responseText);
|
||||||
status = "Authorization completed successfully.";
|
response.ContentLength64 = buffer.Length;
|
||||||
}
|
response.KeepAlive = false;
|
||||||
else
|
|
||||||
{
|
|
||||||
status = "Authorization did not succeed.";
|
|
||||||
}
|
|
||||||
|
|
||||||
string responseText = Resources.OAuthCallbackPage.Replace("{0}", status);
|
using (Stream responseOutput = response.OutputStream)
|
||||||
byte[] buffer = Encoding.UTF8.GetBytes(responseText);
|
{
|
||||||
response.ContentLength64 = buffer.Length;
|
await responseOutput.WriteAsync(buffer, 0, buffer.Length);
|
||||||
response.KeepAlive = false;
|
await responseOutput.FlushAsync();
|
||||||
|
|
||||||
using (Stream responseOutput = response.OutputStream)
|
|
||||||
{
|
|
||||||
await responseOutput.WriteAsync(buffer, 0, buffer.Length);
|
|
||||||
await responseOutput.FlushAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(Code))
|
|
||||||
{
|
|
||||||
return await Task.Run(() => OAuth.GetAccessToken(Code));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
finally
|
||||||
{
|
{
|
||||||
DebugHelper.WriteException(e);
|
Dispose();
|
||||||
e.ShowError();
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(Code))
|
||||||
|
{
|
||||||
|
return await Task.Run(() => OAuth.GetAccessToken(Code));
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in a new issue