-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDropboxClientFactory.cs
More file actions
102 lines (81 loc) · 3.22 KB
/
Copy pathDropboxClientFactory.cs
File metadata and controls
102 lines (81 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Dropbox.Api;
using PneumaticTube.Properties;
namespace PneumaticTube
{
internal static class DropboxClientFactory
{
const string _appkey = "ii29ofre0mjt9vf";
internal static void ResetAuthentication()
{
Settings.Default.ACCESS_TOKEN = string.Empty;
Settings.Default.REFRESH_TOKEN = string.Empty;
Settings.Default.TOKEN_EXPIRATION = string.Empty;
Settings.Default.Save();
}
private static void PersistTokens(OAuth2Response response)
{
Settings.Default.ACCESS_TOKEN = response.AccessToken;
Settings.Default.REFRESH_TOKEN = response.RefreshToken;
Settings.Default.TOKEN_EXPIRATION = response.ExpiresAt.ToString() ?? string.Empty;
Settings.Default.Save();
}
private static bool LoadTokens(out TokenResult result)
{
var refreshToken = Settings.Default.REFRESH_TOKEN;
if(string.IsNullOrEmpty(refreshToken))
{
result = new TokenResult(string.Empty, string.Empty, null);
return false;
}
var accessToken = Settings.Default.ACCESS_TOKEN;
var hasExpiration = DateTime.TryParse(Settings.Default.TOKEN_EXPIRATION, out DateTime expiresAt);
result = new TokenResult(accessToken, refreshToken, hasExpiration ? expiresAt : null);
return true;
}
public static async Task<DropboxClient> CreateDropboxClient(int timeoutSeconds)
{
var result = await GetAccessTokens();
var config = new DropboxClientConfig("PneumaticTube/2")
{
HttpClient = new System.Net.Http.HttpClient
{
Timeout = TimeSpan.FromSeconds(timeoutSeconds)
}
};
if(result.ExpiresAt.HasValue)
{
return new DropboxClient(oauth2AccessToken: result.AccessToken, oauth2RefreshToken: result.RefreshToken,
oauth2AccessTokenExpiresAt: result.ExpiresAt.Value, appKey: _appkey, config: config);
}
return new DropboxClient(oauth2RefreshToken: result.RefreshToken, appKey: _appkey, config: config);
}
private static async Task<TokenResult> GetAccessTokens()
{
if(LoadTokens(out TokenResult tokens))
{
return tokens;
}
Console.WriteLine(
"You'll need to authorize this account with PneumaticTube; a browser window will now open asking you to log into Dropbox and allow the app. When you've done that, you'll be given an access key. Enter the key here and hit Enter:");
var oauthFlow = new PKCEOAuthFlow();
// Pop open the authorization page in the default browser
var url = oauthFlow.GetAuthorizeUri(OAuthResponseType.Code, _appkey, tokenAccessType: TokenAccessType.Offline);
using (Process p = new())
{
p.StartInfo.FileName = url.ToString();
p.StartInfo.UseShellExecute = true;
p.Start();
}
// Wait for the user to enter the code
var code = Console.ReadLine();
var response = await oauthFlow.ProcessCodeFlowAsync(code, _appkey);
// Save the token
PersistTokens(response);
return new TokenResult(response.AccessToken, response.RefreshToken, response.ExpiresAt);
}
record TokenResult(string AccessToken, string RefreshToken, DateTime? ExpiresAt);
}
}