-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleController.cs
267 lines (228 loc) · 9.42 KB
/
GoogleController.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Mastonet.Entities;
using Google.Apis.Tasks.v1.Data;
using Newtonsoft.Json;
using System.Text;
using Microsoft.AspNetCore.Identity;
using GeFeSLE;
using Microsoft.EntityFrameworkCore;
public static class GoogleController
{
private static string GOOGLE_TASKS_API_ME_LISTS_URL = "https://www.googleapis.com/tasks/v1/users/@me/lists";
private static string GOOGLE_TASKS_API_LIST_TASKS_URL = "https://tasks.googleapis.com/tasks/v1/lists/{tasklist}/tasks";
public static string GOOGLE_TASKS_API_TASKS_OAUTH_SCOPE = "https://www.googleapis.com/auth/tasks";
//https://developers.google.com/tasks/reference/rest/v1/tasklists/list
// returns
// {
// "kind": string,
// "etag": string,
// "nextPageToken": string,
// "items": [
// {
// object (TaskList)
// }
// ]
// }
//
// where each Tasklist is
//
// {
// "kind": string,
// "id": string,
// "etag": string,
// "title": string,
// "updated": string,
// "selfLink": string
// }
//
// we need
// title (to show to user)
// id (to get tasks)
public static async Task<List<Google.Apis.Tasks.v1.Data.TaskList>> getGoogleTaskLists(string? accessToken)
{
string fn = "getGoogleTaskLISTS";
DBg.d(LogLevel.Trace, fn);
List<Google.Apis.Tasks.v1.Data.TaskList> listNames = new List<Google.Apis.Tasks.v1.Data.TaskList>();
if (accessToken == null)
{
DBg.d(LogLevel.Error, $"{fn} - No access token found in session");
return listNames;
}
// get the list of task lists
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
DBg.d(LogLevel.Trace, $"{fn} - Getting task lists from {GOOGLE_TASKS_API_ME_LISTS_URL}");
DBg.d(LogLevel.Trace, $"{fn} - Access token: {accessToken}");
var response = await client.GetAsync(GOOGLE_TASKS_API_ME_LISTS_URL);
// handle various response.statusCodes
switch (response.StatusCode)
{
// TODO: add a case in here to check for .Forbidden, which is where the API has to be enabled
// in your Google account. Put instructions on how to do that:
// https://console.developers.google.com/apis/api/tasks.googleapis.com/overview?project=633241786177
case System.Net.HttpStatusCode.OK:
var content = await response.Content.ReadAsStringAsync();
// convert content to dynamic json object
var taskLists = JsonConvert.DeserializeObject<Google.Apis.Tasks.v1.Data.TaskLists>(content);
// Now you can access the task lists through taskLists.Items
if(taskLists == null || taskLists.Items == null)
{
DBg.d(LogLevel.Error, $"{fn} - No task lists found");
return listNames;
}
foreach (TaskList taskList in taskLists.Items)
{
DBg.d(LogLevel.Trace, $"{fn} - Found task list: {taskList.Title} ({taskList.Id})");
listNames.Add(taskList);
}
break;
default:
string error = dumpResponse(response);
DBg.d(LogLevel.Error, $"{fn} - Error getting task lists: {error}");
break;
}
return listNames;
}
public static string dumpResponse(HttpResponseMessage response)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"StatusCode: {response.StatusCode}");
sb.AppendLine($"ReasonPhrase: {response.ReasonPhrase}");
sb.AppendLine($"Headers: {response.Headers}");
sb.AppendLine($"Content: {response.Content}");
string dumpResponse = sb.ToString();
return dumpResponse;
}
public static string dumpRequest(HttpRequestMessage request)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Method: {request.Method}");
sb.AppendLine($"RequestUri: {request.RequestUri}");
sb.AppendLine($"Headers: {request.Headers}");
sb.AppendLine($"Content: {request.Content}");
string dumpRequest = sb.ToString();
return dumpRequest;
}
public static async Task<List<GeListItem>> getGoogleTasks(string listid, string accessToken)
{
string fn = "getGoogleTasks";
DBg.d(LogLevel.Trace, fn);
List<GeListItem> geListItems = new List<GeListItem>();
if (accessToken == null)
{
DBg.d(LogLevel.Error, $"{fn} - No access token found in session");
return geListItems;
}
if (listid == null)
{
DBg.d(LogLevel.Error, $"{fn} - No list id given");
return geListItems;
}
// get the list of task lists
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
var url = GOOGLE_TASKS_API_LIST_TASKS_URL;
// replace the "{tasklist}" in the url with the listId
url = url.Replace("{tasklist}", listid);
var response = await client.GetAsync(url);
// handle various response.statusCodes
switch (response.StatusCode)
{
case System.Net.HttpStatusCode.OK:
var content = await response.Content.ReadAsStringAsync();
var listOfTasks = JsonConvert.DeserializeObject<Google.Apis.Tasks.v1.Data.Tasks>(content);
// Now you can access the task lists through taskLists.Items
foreach (Google.Apis.Tasks.v1.Data.Task task in listOfTasks.Items)
{
GeListItem newItem = parseGoogleTask(task);
geListItems.Add(newItem);
}
break;
default:
DBg.d(LogLevel.Error, $"{fn} - Error getting task lists: {response.StatusCode}");
break;
}
return geListItems;
}
// {
// "completed": null,
// "deleted": null,
// "due": null,
// "etag": "\"MTc0ODI3MDg0Nw\"",
// "hidden": null,
// "id": "MTAyNzkyMjI5MDU5NzY5NDE0MjA6MDo1NDYxOTE0MzQ",
// "kind": "tasks#task",
// "links": [],
// "notes": null,
// "parent": null,
// "position": "00000000000000000001",
// "selfLink": "https://www.googleapis.com/tasks/v1/lists/MTAyNzkyMjI5MDU5NzY5NDE0MjA6MDow/tasks/MTAyNzkyMjI5MDU5NzY5NDE0MjA6MDo1NDYxOTE0MzQ",
// "status": "needsAction",
// "title": "",
// "updated": "2019-06-18T01:29:59.000Z",
// "webViewLink": "https://tasks.google.com/task/10279222905976941420:0:546191434"
// }
private static GeListItem parseGoogleTask(Google.Apis.Tasks.v1.Data.Task task)
{
string fn = "parseGoogleTask";
DBg.d(LogLevel.Trace, fn);
if (task == null)
{
DBg.d(LogLevel.Error, $"{fn} - task is null");
return null;
}
// deserialize task and print it
Console.WriteLine(JsonConvert.SerializeObject(task, Formatting.Indented));
GeListItem newItem = new GeListItem();
newItem.Name = task.Title;
newItem.Comment = task.Notes;
//newItem. = task.Due;
// newItem.Completed = task.Status == "completed";
return newItem;
}
// Add this line
// Add other necessary using directives
public static async Task<StringBuilder> makeTaskListChooser(List<(string, string)> taskLists,
GeFeSLEDb db,
HttpContext httpContext,
UserManager<GeFeSLEUser> userManager,
GeFeSLEUser me)
{
string fn = "makeTaskListChooser";
DBg.d(LogLevel.Trace, fn);
StringBuilder sb = new StringBuilder();
await GlobalStatic.GenerateHTMLHead(sb, "Choose a Task List");
sb.AppendLine("<h1>Choose a Google Task List & Destination List</h1>");
sb.AppendLine("<p>Source (Google) task list</p>");
sb.AppendLine("<form>");
sb.AppendLine("<select name=\"sourceList\">");
foreach ((string, string) taskList in taskLists)
{
sb.AppendLine($"<option value=\"{taskList.Item2}\">{taskList.Item1}</opton>");
}
sb.AppendLine("</select>");
sb.AppendLine("<p>Destination list</p>");
sb.AppendLine("<select name=\"destinationList\">");
List<GeList> lists = await db.Lists.ToListAsync();
IList<string> myRoles = await userManager.GetRolesAsync(me);
foreach (GeList list in lists)
{
(bool canISee, string? ynot) = ProtectedFiles.IsListVisibleToUser(list, me, myRoles);
if (canISee)
{
sb.AppendLine($"<option value=\"{list.Id}\">{list.Name}</option>");
}
}
sb.AppendLine("</select>");
sb.AppendLine("</form>");
sb.AppendLine("<script>");
sb.AppendLine("function redirectToImport() {");
sb.AppendLine(" var sourceList = document.getElementsByName('sourceList')[0].value;");
sb.AppendLine(" var destinationList = document.getElementsByName('destinationList')[0].value;");
sb.AppendLine(" window.location.href = '/googletasklistimport/' + sourceList + '/' + destinationList;");
sb.AppendLine("}");
sb.AppendLine("</script>");
sb.AppendLine("<button onclick='redirectToImport()'>Import</button>");
await GlobalStatic.GeneratePageFooter(sb);
return sb;
}
}