| 1 |
// |
| 2 |
// MainPage.xaml.cpp |
| 3 |
// Implementation of the MainPage class. |
| 4 |
// |
| 5 |
|
| 6 |
#include "pch.h" |
| 7 |
|
| 8 |
#include "MainPage.xaml.h" |
| 9 |
|
| 10 |
using namespace OAuth2Live; |
| 11 |
|
| 12 |
using namespace Concurrency; |
| 13 |
using namespace Platform; |
| 14 |
using namespace Windows::Foundation; |
| 15 |
using namespace Windows::Foundation::Collections; |
| 16 |
using namespace Windows::UI::Xaml; |
| 17 |
using namespace Windows::UI::Xaml::Controls; |
| 18 |
using namespace Windows::UI::Xaml::Controls::Primitives; |
| 19 |
using namespace Windows::UI::Xaml::Data; |
| 20 |
using namespace Windows::UI::Xaml::Input; |
| 21 |
using namespace Windows::UI::Xaml::Media; |
| 22 |
using namespace Windows::UI::Xaml::Navigation; |
| 23 |
using namespace Windows::Security::Authentication::Web; |
| 24 |
|
| 25 |
using namespace web::http; |
| 26 |
using namespace web::http::client; |
| 27 |
using namespace web::http::oauth2::experimental; |
| 28 |
|
| 29 |
// |
| 30 |
// NOTE: You must set this Live key and secret for app to work. |
| 31 |
// |
| 32 |
static const utility::string_t s_live_key; |
| 33 |
static const utility::string_t s_live_secret; |
| 34 |
|
| 35 |
MainPage::MainPage() |
| 36 |
: m_live_oauth2_config(s_live_key, |
| 37 |
s_live_secret, |
| 38 |
L"https://login.live.com/oauth20_authorize.srf", |
| 39 |
L"https://login.live.com/oauth20_token.srf", |
| 40 |
L"https://login.live.com/oauth20_desktop.srf") |
| 41 |
{ |
| 42 |
InitializeComponent(); |
| 43 |
} |
| 44 |
|
| 45 |
/// <summary> |
| 46 |
/// Invoked when this page is about to be displayed in a Frame. |
| 47 |
/// </summary> |
| 48 |
/// <param name="e">Event data that describes how this page was reached. The Parameter |
| 49 |
/// property is typically used to configure the page.</param> |
| 50 |
void MainPage::OnNavigatedTo(NavigationEventArgs ^ e) |
| 51 |
{ |
| 52 |
(void)e; // Unused parameter |
| 53 |
} |
| 54 |
|
| 55 |
void OAuth2Live::MainPage::_UpdateButtonState() |
| 56 |
{ |
| 57 |
const bool has_access_token = !m_live_oauth2_config.token().access_token().empty(); |
| 58 |
GetInfoButton->IsEnabled = has_access_token; |
| 59 |
GetContactsButton->IsEnabled = has_access_token; |
| 60 |
GetEventsButton->IsEnabled = has_access_token; |
| 61 |
|
| 62 |
const bool has_refresh_token = !m_live_oauth2_config.token().refresh_token().empty(); |
| 63 |
RefreshTokenButton->IsEnabled = has_refresh_token; |
| 64 |
} |
| 65 |
|
| 66 |
void OAuth2Live::MainPage::_GetToken() |
| 67 |
{ |
| 68 |
m_live_oauth2_config.set_scope(L"wl.basic wl.calendars"); |
| 69 |
|
| 70 |
// Start over, clear tokens and button state. |
| 71 |
m_live_oauth2_config.set_token(oauth2_token()); |
| 72 |
AccessToken->Text.clear(); |
| 73 |
_UpdateButtonState(); |
| 74 |
|
| 75 |
String ^ authURI = ref new String(m_live_oauth2_config.build_authorization_uri(true).c_str()); |
| 76 |
auto startURI = ref new Uri(authURI); |
| 77 |
String ^ redirectURI = ref new String(m_live_oauth2_config.redirect_uri().c_str()); |
| 78 |
auto endURI = ref new Uri(redirectURI); |
| 79 |
|
| 80 |
try |
| 81 |
{ |
| 82 |
DebugArea->Text += "> Navigating WebAuthenticationBroker to " + authURI + "\n"; |
| 83 |
|
| 84 |
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP |
| 85 |
WebAuthenticationBroker::AuthenticateAndContinue(startURI, endURI, nullptr, WebAuthenticationOptions::None); |
| 86 |
#else |
| 87 |
concurrency::create_task( |
| 88 |
WebAuthenticationBroker::AuthenticateAsync(WebAuthenticationOptions::None, startURI, endURI)) |
| 89 |
.then([this](WebAuthenticationResult ^ result) { |
| 90 |
String ^ statusString; |
| 91 |
|
| 92 |
DebugArea->Text += "< WebAuthenticationBroker returned: "; |
| 93 |
switch (result->ResponseStatus) |
| 94 |
{ |
| 95 |
case WebAuthenticationStatus::ErrorHttp: |
| 96 |
{ |
| 97 |
DebugArea->Text += "ErrorHttp: " + result->ResponseErrorDetail + "\n"; |
| 98 |
break; |
| 99 |
} |
| 100 |
case WebAuthenticationStatus::Success: |
| 101 |
{ |
| 102 |
DebugArea->Text += "Success\n"; |
| 103 |
utility::string_t data = result->ResponseData->Data(); |
| 104 |
DebugArea->Text += "Redirected URI:\n" + result->ResponseData + "\n"; |
| 105 |
DebugArea->Text += "> Obtaining token using the redirected URI\n"; |
| 106 |
m_live_oauth2_config.token_from_redirected_uri(data).then( |
| 107 |
[this](pplx::task<void> token_task) { |
| 108 |
try |
| 109 |
{ |
| 110 |
token_task.wait(); |
| 111 |
DebugArea->Text += "< Got token\n"; |
| 112 |
AccessToken->Text = |
| 113 |
ref new String(m_live_oauth2_config.token().access_token().c_str()); |
| 114 |
} |
| 115 |
catch (const oauth2_exception& e) |
| 116 |
{ |
| 117 |
DebugArea->Text += "< Failed to get token\n"; |
| 118 |
String ^ error = |
| 119 |
ref new String(utility::conversions::to_string_t(e.what()).c_str()); |
| 120 |
DebugArea->Text += "Error: " + error + "\n"; |
| 121 |
} |
| 122 |
}, |
| 123 |
pplx::task_continuation_context::use_current()); |
| 124 |
break; |
| 125 |
} |
| 126 |
default: |
| 127 |
case WebAuthenticationStatus::UserCancel: |
| 128 |
{ |
| 129 |
DebugArea->Text += "UserCancel\n"; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
}); |
| 134 |
#endif |
| 135 |
} |
| 136 |
catch (Exception ^ ex) |
| 137 |
{ |
| 138 |
DebugArea->Text += "< Error launching WebAuthenticationBroker: " + ex->Message + "\n"; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void OAuth2Live::MainPage::GetTokenButtonClick(Platform::Object ^ sender, |
| 143 |
Windows::UI::Xaml::Navigation::NavigationEventArgs ^ e) |
| 144 |
{ |
| 145 |
if (m_live_oauth2_config.client_key().empty() || m_live_oauth2_config.client_secret().empty()) |
| 146 |
{ |
| 147 |
DebugArea->Text += |
| 148 |
"Error: Cannot get token because Live app key or secret is empty. Please see instructions.\n"; |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
_GetToken(); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
void OAuth2Live::MainPage::GetInfoButtonClick(Platform::Object ^ sender, |
| 157 |
Windows::UI::Xaml::Navigation::NavigationEventArgs ^ e) |
| 158 |
{ |
| 159 |
DebugArea->Text += "> Get user info\n"; |
| 160 |
m_live_client->request(methods::GET, U("me")) |
| 161 |
.then([](http_response resp) { return resp.extract_json(); }) |
| 162 |
.then( |
| 163 |
[this](web::json::value j) -> void { |
| 164 |
String ^ json_code = ref new String(j.serialize().c_str()); |
| 165 |
DebugArea->Text += "< User info (JSON): " + json_code + "\n"; |
| 166 |
}, |
| 167 |
pplx::task_continuation_context::use_current()); |
| 168 |
} |
| 169 |
|
| 170 |
void OAuth2Live::MainPage::GetContactsButtonClick(Platform::Object ^ sender, |
| 171 |
Windows::UI::Xaml::Navigation::NavigationEventArgs ^ e) |
| 172 |
{ |
| 173 |
DebugArea->Text += "> Get user contacts\n"; |
| 174 |
m_live_client->request(methods::GET, U("me/contacts")) |
| 175 |
.then([](http_response resp) { return resp.extract_json(); }) |
| 176 |
.then( |
| 177 |
[this](web::json::value j) -> void { |
| 178 |
String ^ json_code = ref new String(j.serialize().c_str()); |
| 179 |
DebugArea->Text += "< User contacts (JSON): " + json_code + "\n"; |
| 180 |
}, |
| 181 |
pplx::task_continuation_context::use_current()); |
| 182 |
} |
| 183 |
|
| 184 |
void OAuth2Live::MainPage::GetEventsButtonClick(Platform::Object ^ sender, |
| 185 |
Windows::UI::Xaml::Navigation::NavigationEventArgs ^ e) |
| 186 |
{ |
| 187 |
DebugArea->Text += "> Get user events\n"; |
| 188 |
m_live_client->request(methods::GET, U("me/events")) |
| 189 |
.then([](http_response resp) { return resp.extract_json(); }) |
| 190 |
.then( |
| 191 |
[this](web::json::value j) -> void { |
| 192 |
String ^ json_code = ref new String(j.serialize().c_str()); |
| 193 |
DebugArea->Text += "< User calendar events (JSON): " + json_code + "\n"; |
| 194 |
}, |
| 195 |
pplx::task_continuation_context::use_current()); |
| 196 |
} |
| 197 |
|
| 198 |
void OAuth2Live::MainPage::AccessTokenTextChanged(Platform::Object ^ sender, |
| 199 |
Windows::UI::Xaml::Controls::TextChangedEventArgs ^ e) |
| 200 |
{ |
| 201 |
http_client_config http_config; |
| 202 |
http_config.set_oauth2(m_live_oauth2_config); |
| 203 |
m_live_client.reset(new http_client(U("https://apis.live.net/v5.0/"), http_config)); |
| 204 |
_UpdateButtonState(); |
| 205 |
} |
| 206 |
|
| 207 |
void OAuth2Live::MainPage::ImplicitGrantUnchecked(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) |
| 208 |
{ |
| 209 |
m_live_oauth2_config.set_implicit_grant(false); |
| 210 |
} |
| 211 |
|
| 212 |
void OAuth2Live::MainPage::ImplicitGrantChecked(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) |
| 213 |
{ |
| 214 |
m_live_oauth2_config.set_implicit_grant(true); |
| 215 |
} |
| 216 |
|
| 217 |
void OAuth2Live::MainPage::RefreshTokenButtonClick(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) |
| 218 |
{ |
| 219 |
DebugArea->Text += "> Refreshing token\n"; |
| 220 |
|
| 221 |
m_live_oauth2_config.token_from_refresh().then( |
| 222 |
[this](pplx::task<void> refresh_task) { |
| 223 |
try |
| 224 |
{ |
| 225 |
refresh_task.wait(); |
| 226 |
DebugArea->Text += "< Got token\n"; |
| 227 |
AccessToken->Text = ref new String(m_live_oauth2_config.token().access_token().c_str()); |
| 228 |
} |
| 229 |
catch (const oauth2_exception& e) |
| 230 |
{ |
| 231 |
DebugArea->Text += "< Failed to get token\n"; |
| 232 |
String ^ error = ref new String(utility::conversions::to_string_t(e.what()).c_str()); |
| 233 |
DebugArea->Text += "Error: " + error + "\n"; |
| 234 |
} |
| 235 |
}, |
| 236 |
pplx::task_continuation_context::use_current()); |
| 237 |
} |