Files
@ f2a6ba12fc29
Branch filter:
Location: libtransport.git/3rdparty/cpprestsdk/samples/OAuth2Live/MainPage.xaml.cpp
f2a6ba12fc29
8.7 KiB
text/x-c++hdr
Slack frontend stub
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 | //
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//
#include "pch.h"
#include "MainPage.xaml.h"
using namespace OAuth2Live;
using namespace Concurrency;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::Security::Authentication::Web;
using namespace web::http;
using namespace web::http::client;
using namespace web::http::oauth2::experimental;
//
// NOTE: You must set this Live key and secret for app to work.
//
static const utility::string_t s_live_key(U(""));
static const utility::string_t s_live_secret(U(""));
MainPage::MainPage()
: m_live_oauth2_config(
s_live_key,
s_live_secret,
L"https://login.live.com/oauth20_authorize.srf",
L"https://login.live.com/oauth20_token.srf",
L"https://login.live.com/oauth20_desktop.srf")
{
InitializeComponent();
}
/// <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. The Parameter
/// property is typically used to configure the page.</param>
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
(void) e; // Unused parameter
}
void OAuth2Live::MainPage::_UpdateButtonState()
{
const bool has_access_token = !m_live_oauth2_config.token().access_token().empty();
GetInfoButton->IsEnabled = has_access_token;
GetContactsButton->IsEnabled = has_access_token;
GetEventsButton->IsEnabled = has_access_token;
const bool has_refresh_token = !m_live_oauth2_config.token().refresh_token().empty();
RefreshTokenButton->IsEnabled = has_refresh_token;
}
void OAuth2Live::MainPage::_GetToken()
{
m_live_oauth2_config.set_scope(L"wl.basic wl.calendars");
// Start over, clear tokens and button state.
m_live_oauth2_config.set_token(oauth2_token());
AccessToken->Text = "";
_UpdateButtonState();
String^ authURI = ref new String(m_live_oauth2_config.build_authorization_uri(true).c_str());
auto startURI = ref new Uri(authURI);
String^ redirectURI = ref new String(m_live_oauth2_config.redirect_uri().c_str());
auto endURI = ref new Uri(redirectURI);
try
{
DebugArea->Text += "> Navigating WebAuthenticationBroker to " + authURI + "\n";
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
WebAuthenticationBroker::AuthenticateAndContinue(startURI, endURI, nullptr, WebAuthenticationOptions::None);
#else
concurrency::create_task(WebAuthenticationBroker::AuthenticateAsync(WebAuthenticationOptions::None, startURI, endURI))
.then([this](WebAuthenticationResult^ result)
{
String^ statusString;
DebugArea->Text += "< WebAuthenticationBroker returned: ";
switch (result->ResponseStatus)
{
case WebAuthenticationStatus::ErrorHttp:
{
DebugArea->Text += "ErrorHttp: " + result->ResponseErrorDetail + "\n";
break;
}
case WebAuthenticationStatus::Success:
{
DebugArea->Text += "Success\n";
utility::string_t data = result->ResponseData->Data();
DebugArea->Text += "Redirected URI:\n" + result->ResponseData + "\n";
DebugArea->Text += "> Obtaining token using the redirected URI\n";
m_live_oauth2_config.token_from_redirected_uri(data).then([this](pplx::task<void> token_task)
{
try
{
token_task.wait();
DebugArea->Text += "< Got token\n";
AccessToken->Text = ref new String(m_live_oauth2_config.token().access_token().c_str());
}
catch (const oauth2_exception& e)
{
DebugArea->Text += "< Failed to get token\n";
String^ error = ref new String(utility::conversions::to_string_t(e.what()).c_str());
DebugArea->Text += "Error: " + error + "\n";
}
}, pplx::task_continuation_context::use_current());
break;
}
default:
case WebAuthenticationStatus::UserCancel:
{
DebugArea->Text += "UserCancel\n";
break;
}
}
});
#endif
}
catch (Exception^ ex)
{
DebugArea->Text += "< Error launching WebAuthenticationBroker: " + ex->Message + "\n";
}
}
void OAuth2Live::MainPage::GetTokenButtonClick(Platform::Object^ sender, Windows::UI::Xaml::Navigation::NavigationEventArgs^ e)
{
if (m_live_oauth2_config.client_key().empty() || m_live_oauth2_config.client_secret().empty())
{
DebugArea->Text += "Error: Cannot get token because Live app key or secret is empty. Please see instructions.\n";
}
else
{
_GetToken();
}
}
void OAuth2Live::MainPage::GetInfoButtonClick(Platform::Object^ sender, Windows::UI::Xaml::Navigation::NavigationEventArgs^ e)
{
DebugArea->Text += "> Get user info\n";
m_live_client->request(methods::GET, U("me"))
.then([](http_response resp)
{
return resp.extract_json();
})
.then([this](web::json::value j) -> void
{
String^ json_code = ref new String(j.serialize().c_str());
DebugArea->Text += "< User info (JSON): " + json_code + "\n";
}, pplx::task_continuation_context::use_current());
}
void OAuth2Live::MainPage::GetContactsButtonClick(Platform::Object^ sender, Windows::UI::Xaml::Navigation::NavigationEventArgs^ e)
{
DebugArea->Text += "> Get user contacts\n";
m_live_client->request(methods::GET, U("me/contacts"))
.then([](http_response resp)
{
return resp.extract_json();
})
.then([this](web::json::value j) -> void
{
String^ json_code = ref new String(j.serialize().c_str());
DebugArea->Text += "< User contacts (JSON): " + json_code + "\n";
}, pplx::task_continuation_context::use_current());
}
void OAuth2Live::MainPage::GetEventsButtonClick(Platform::Object^ sender, Windows::UI::Xaml::Navigation::NavigationEventArgs^ e)
{
DebugArea->Text += "> Get user events\n";
m_live_client->request(methods::GET, U("me/events"))
.then([](http_response resp)
{
return resp.extract_json();
})
.then([this](web::json::value j) -> void
{
String^ json_code = ref new String(j.serialize().c_str());
DebugArea->Text += "< User calendar events (JSON): " + json_code + "\n";
}, pplx::task_continuation_context::use_current());
}
void OAuth2Live::MainPage::AccessTokenTextChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::TextChangedEventArgs^ e)
{
http_client_config http_config;
http_config.set_oauth2(m_live_oauth2_config);
m_live_client.reset(new http_client(U("https://apis.live.net/v5.0/"), http_config));
_UpdateButtonState();
}
void OAuth2Live::MainPage::ImplicitGrantUnchecked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
m_live_oauth2_config.set_implicit_grant(false);
}
void OAuth2Live::MainPage::ImplicitGrantChecked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
m_live_oauth2_config.set_implicit_grant(true);
}
void OAuth2Live::MainPage::RefreshTokenButtonClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
DebugArea->Text += "> Refreshing token\n";
m_live_oauth2_config.token_from_refresh().then([this](pplx::task<void> refresh_task)
{
try
{
refresh_task.wait();
DebugArea->Text += "< Got token\n";
AccessToken->Text = ref new String(m_live_oauth2_config.token().access_token().c_str());
}
catch (const oauth2_exception& e)
{
DebugArea->Text += "< Failed to get token\n";
String^ error = ref new String(utility::conversions::to_string_t(e.what()).c_str());
DebugArea->Text += "Error: " + error + "\n";
}
}, pplx::task_continuation_context::use_current());
}
|