/
MainPage.xaml.cs
153 lines (135 loc) · 5.78 KB
/
MainPage.xaml.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
/**
* JBoss, Home of Professional Open Source
* Copyright Red Hat, Inc., and individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using AeroGear.OAuth2;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641
namespace demo
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page, IFileOpenPickerContinuable, IWebAuthenticationContinuable
{
public static MainPage Current;
public StorageFile file { get; set; }
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
Current = this;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.
// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (file == null)
{
var openPicker = new FileOpenPicker
{
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
ViewMode = PickerViewMode.Thumbnail
};
openPicker.FileTypeFilter.Add(".jpg");
openPicker.PickSingleFileAndContinue();
button.Content = "Upload";
}
else
{
var config = await GoogleConfig.Create(
"517285908032-12332132132131321312.apps.googleusercontent.com",
new List<string>(new string[] { "https://www.googleapis.com/auth/drive" }),
"google"
);
//var config = await KeycloakConfig.Create("shoot-third-party", "https://localhost:8443", "shoot-realm");
//var config = FacebookConfig.Create("1654557457742519", "9cab3cb953d3194908f44f1764b5b921",
// new List<string>(new string[] { "photo_upload, publish_actions" }), "facebook");
var module = await AccountManager.AddAccount(config);
if (await module.RequestAccessAndContinue())
{
Upload(module);
}
}
}
public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
if (args.Files.Count > 0)
{
file = args.Files[0];
}
else
{
button.Content = "Take Picture";
await new MessageDialog("no file to upload").ShowAsync();
}
}
public async void Upload(OAuth2Module module)
{
using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
{
button.IsEnabled = false;
progress.IsActive = true;
client.DefaultRequestHeaders.Authorization = module.AuthenticationHeaderValue();
var fileContent = new StreamContent((await file.OpenAsync(FileAccessMode.Read)).AsStreamForRead());
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"image\"",
FileName = "\"" + file.Name + "\""
};
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
content.Add(fileContent);
HttpResponseMessage response = await client.PostAsync(new Uri("https://www.googleapis.com/upload/drive/v2/files"), content);
string responseString = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseString);
await new MessageDialog("uploaded file " + (response.StatusCode != HttpStatusCode.OK ? "un" : "") + "successful").ShowAsync();
}
file = null;
button.Content = "Take Picture";
button.IsEnabled = true;
progress.IsActive = false;
}
async void IWebAuthenticationContinuable.ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args)
{
Upload(await AccountManager.ParseContinuationEvent(args));
}
}
}