-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnedrive.cs
164 lines (154 loc) · 7.83 KB
/
Onedrive.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.IO;
using System.Net.Http;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Text.Json;
using System.Xml;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using Microsoft.Graph.Auth;
using System.Linq;
using Microsoft.Extensions.Logging;
using System.Text.RegularExpressions;
using PushOver;
using Microsoft.Kiota.Abstractions.Authentication;
namespace AntiTeleBot
{
public class TokenProvider : IAccessTokenProvider
{
public Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object> additionalAuthenticationContext = default,
CancellationToken cancellationToken = default)
{
//var token = "token";
// get the token and return it
return Task.FromResult(token);
}
public AllowedHostsValidator AllowedHostsValidator { get; }
public string token = "";
}
public static class Onedrive
{
public static string clientId;
private static IPublicClientApplication pca;
private static ILogger log;
private const string Authority = "https://login.microsoftonline.com/consumers";
private static string[] scopes = new string[] { "User.Read", "Files.Read.All", "Files.ReadWrite.All" };
static async Task<AuthenticationResult> GetATokenForGraphDeviceCode()
{
var accounts = await pca.GetAccountsAsync();
var result = await AcquireByDeviceCodeAsync(pca);
return result;
}
private static async Task<AuthenticationResult> AcquireByDeviceCodeAsync(IPublicClientApplication pca)
{
var result = await pca.AcquireTokenWithDeviceCode(scopes,
deviceCodeResult =>
{
log.LogInformation(deviceCodeResult.Message);
if (System.Environment.GetEnvironmentVariable("PushOverUserId") != "")
{
PushOverSender.SendPushMessage(System.Environment.GetEnvironmentVariable("PushOverUserId"),
System.Environment.GetEnvironmentVariable("PushOverAppTokenId"), "AntiTeleBot", deviceCodeResult.Message);
}
Console.WriteLine(deviceCodeResult.Message);
return Task.FromResult(0);
}).ExecuteAsync();
Console.WriteLine(result.Account.Username);
return result;
}
public static async Task UploadFileToOnedrive(ILogger logger, string recordingUrl, string caller, string LogFullFileName)
{
log = logger;
recordingUrl = recordingUrl + ".mp3";
clientId = System.Environment.GetEnvironmentVariable("OnedriveApplicationCliendId");
string token = "";
string FolderName = System.Environment.GetEnvironmentVariable("OnedriveFolderName");
pca = PublicClientApplicationBuilder
.Create(clientId)
.WithAuthority(Authority)
.WithDefaultRedirectUri()
.Build();
TokenCacheHelper.EnableSerialization(pca.UserTokenCache, log);
GraphServiceClient graphClient = null;
try
{
var accounts = await pca.GetAccountsAsync();
var tokenProvider = new TokenProvider();
var account = await pca.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();
tokenProvider.token = account.AccessToken;
token = account.AccessToken;
var authenticationProvider = new BaseBearerTokenAuthenticationProvider(tokenProvider);
graphClient = new GraphServiceClient(authenticationProvider);
//graphClient.auth
/* graphClient.AuthenticationProvider = new DelegateAuthenticationProvider(async (request) =>
{
token = pca.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync().Result.AccessToken;
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
await Task.FromResult(0);
}); */
var driveItem = await graphClient.Me.Drive.GetAsync();
//await graphClient.Me.Drive. Request().GetAsync();
}
catch
{
token = "";
log.LogInformation("Onedrive token invalid. Clearing");
}
if (token == "")
{
var a = await GetATokenForGraphDeviceCode();
token = a.AccessToken;
log.LogInformation("NEW TOKEN: " + token);
var tokenProvider = new TokenProvider();
tokenProvider.token = token;
var authenticationProvider = new BaseBearerTokenAuthenticationProvider(tokenProvider);
graphClient = new GraphServiceClient(authenticationProvider);
}
/* graphClient.AuthenticationProvider = new DelegateAuthenticationProvider(async (request) =>
{
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
await Task.FromResult<object>(null);
}); */
var t = Regex.Split(recordingUrl, "/");
log.LogInformation(recordingUrl);
var filename = caller.Replace("+", "") + "_" + DateTime.Now.ToString("s").Replace(":", ".") + ".mp3";
var h = new HttpClient();
var hresponse = await h.GetAsync(recordingUrl);
Stream streamToReadFrom = await hresponse.Content.ReadAsStreamAsync();
var drive = await graphClient.Me.Drive.GetAsync();
//var drive = await graphClient.Me.Drive.Request().GetAsync();
var items = await graphClient.Drives[drive.Id].Items["root"].Children.GetAsync();
var folder = items.Value.Where(f => f.Name.ToLower() == FolderName.ToLower()).FirstOrDefault();
if (folder == null)
{
var driveItem = new Microsoft.Graph.Models.DriveItem
{
Name = FolderName,
Folder = new Microsoft.Graph.Models.Folder
{
}
};
folder = items.Value.Where(f => f.Name.ToLower() == FolderName.ToLower()).FirstOrDefault();
folder.Children.Add(driveItem);
items = await graphClient.Drives[drive.Id].Items["root"].Children.GetAsync();
folder = items.Value.Where(f => f.Name.ToLower() == FolderName.ToLower()).FirstOrDefault();
}
//using var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(@"The contents of the file goes here."));
//var stream = new System.IO.FileStream(@"C:\_MY_DATA_\GIT_REPOS\a.json", System.IO.FileMode.Open);
await graphClient.Drives[drive.Id].Items[folder.Id].ItemWithPath(filename).Content.PutAsync(streamToReadFrom);
log.LogInformation($"log location: {LogFullFileName}");
string newlogfilename = filename.Replace(".mp3", ".txt");
Stream streamLogFile = System.IO.File.OpenRead(LogFullFileName);
//System.IO.File.Move(LogFullFileName,@$"{System.IO.Path.GetTempPath()}\{newlogfilename}" );
await graphClient.Drives[drive.Id].Items[folder.Id].ItemWithPath(newlogfilename).Content.PutAsync(streamLogFile);
if (System.Environment.GetEnvironmentVariable("PushOverUserId") != "")
{
PushOverSender.SendPushMessage(System.Environment.GetEnvironmentVariable("PushOverUserId"),
System.Environment.GetEnvironmentVariable("PushOverAppTokenId"), "AntiTeleBot", "Nowe nagranie na OneDrive od " + caller);
}
}
}
}