Debian Codegraph @

Release/samples/FacebookDemo/Facebook.cpp

raw
1 /***
2 * Copyright (C) Microsoft. All rights reserved.
3 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4 *
5 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6 *
7 * Facebook.cpp - Implementation file for simple facebook client.
8 * Note: this implementation will not work until you replace the placeholder
9 * strings below with tokens obtained from facebook.
10 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
11 ****/
12
13 #include "pch.h"
14
15 #include "facebook.h"
16
17 using namespace pplx;
18 using namespace web;
19 using namespace Platform;
20 using namespace Windows::Foundation;
21 using namespace Windows::Security::Authentication::Web;
22 using namespace Windows::Storage;
23
24 // Placeholder strings for app tokens
25 // see blog post at http://aka.ms/FacebookCppRest
26 // for information on obtaining these values
27 const std::wstring application_id(L"insert your application ID");
28 const std::wstring application_token(L"insert your application token");
29
30 facebook_client& facebook_client::instance()
31 {
32 static facebook_client c;
33 return c;
34 }
35
36 pplx::task<void> facebook_client::login(std::wstring scopes)
37 {
38 // Look in the Local Settings for previously-stored login credentials
39 auto ls = ApplicationData::Current->LocalSettings->CreateContainer("LoginDetailsCache",
40 ApplicationDataCreateDisposition::Always);
41
42 if (ls->Values->HasKey("facebookToken"))
43 {
44 token_ = dynamic_cast<String ^>(ls->Values->Lookup("facebookToken"))->Data();
45 }
46
47 if (!token_.empty())
48 {
49 // Check if the token is still valid
50 using namespace http;
51
52 uri_builder tokendbg_uri(L"/debug_token");
53 tokendbg_uri.append_query(L"input_token", token_);
54 tokendbg_uri.append_query(L"access_token", application_token);
55
56 http_request request(methods::GET);
57 request.set_request_uri(tokendbg_uri.to_string());
58 request.headers().add(header_names::accept, L"application/json");
59
60 return raw_client.request(request)
61 .then([](http_response response) { return response.extract_json(); })
62 .then(
63 [=](json::value v) -> task<void> {
64 auto obj = v[L"data"];
65
66 if (obj[L"is_valid"].as_bool())
67 {
68 // Currently cached access token is valid
69 signed_in = true;
70 return create_task([]() {}); // Return an empty task to match the function's return value
71 }
72
73 // If the token was invalid, go through full login
74 return full_login(scopes);
75 },
76 task_continuation_context::use_current());
77 }
78
79 // If no token was found, go through full login
80 return full_login(scopes);
81 }
82
83 pplx::task<void> facebook_client::full_login(std::wstring scopes)
84 {
85 // Create uri for OAuth login on Facebook
86 http::uri_builder login_uri(L"https://www.facebook.com/dialog/oauth");
87 login_uri.append_query(L"client_id", application_id); // App id
88 login_uri.append_query(L"redirect_uri", L"https://www.facebook.com/connect/login_success.html");
89 login_uri.append_query(L"scope", scopes);
90 login_uri.append_query(L"display", L"popup");
91 login_uri.append_query(L"response_type", L"token");
92
93 return create_task(WebAuthenticationBroker::AuthenticateAsync(
94 WebAuthenticationOptions::None,
95 ref new Uri(ref new String(login_uri.to_string().c_str())),
96 ref new Uri("https://www.facebook.com/connect/login_success.html")))
97 .then([=](WebAuthenticationResult ^ result) {
98 if (result->ResponseStatus == WebAuthenticationStatus::Success)
99 {
100 signed_in = true;
101
102 std::wstring response(result->ResponseData->Data()); // Save authentication token
103 auto start = response.find(L"access_token=");
104 start += 13;
105 auto end = response.find('&');
106
107 token_ = response.substr(start, end - start);
108
109 // Add token to Local Settings for future login attempts
110 auto ls = ApplicationData::Current->LocalSettings->CreateContainer(
111 "LoginDetailsCache", ApplicationDataCreateDisposition::Always);
112
113 ls->Values->Insert("facebookToken", ref new String(token_.c_str()));
114 }
115 });
116 }
117
118 task<json::value> facebook_client::get(std::wstring path)
119 {
120 using namespace http;
121
122 http_request request(methods::GET);
123
124 request.set_request_uri(base_uri().append_path(path).to_uri());
125 request.headers().add(header_names::accept, L"application/json");
126
127 return raw_client.request(request).then([](http_response response) { return response.extract_json(); });
128 }
129
130 http::uri_builder facebook_client::base_uri(bool absolute)
131 {
132 http::uri_builder ret;
133
134 if (absolute) ret.set_path(L"https://graph.facebook.com");
135
136 ret.append_query(L"access_token", token_);
137 return ret;
138 }