Connecting to Facebook with the C++ REST SDK

Andy Rich

Hi, this is Andy Rich from the C++ QA team.  Previously, I showed you how you can use the C++ REST API to connect to Live services, but this is just one of many web services that you can use the REST API with.  In this blog post, I will walk you through creating a Windows 8 Store App that will log a user into Facebook and download and display their photo albums.

Setting up a Facebook Developer Account

In order to develop apps for Facebook, you will need to have a real Facebook account, and sign up to be a developer on their platform, which you can accomplish by visiting http://developers.facebook.com.

Once you are enrolled as a developer, you will need to create an app identity by clicking “Apps” in the top bar and choosing “Create New App”.  Give your app a name, but don’t worry about setting any of the other parameters for now.  Click “Save Changes” and your app will be created.

You should also set your app to a “Desktop” app, which will reduce how often your users need to log in.  To do this, select “Advanced” under your application settings, change App Type to “Native/Desktop” and set “App Secret in Client” to “No” and click the “Save Changes” button.

Finally, go your basic settings page and make note of your app’s App ID/API key (later referred to as <appid>) and your app secret (later called <appsecret>).  We will need these values in future steps.

For the purposes of this post, we will be creating an abstraction to deal with communication with Facebook, a facebook_client class:

// Facebook.h

#pragma once

#include <string>

#include <http_client.h>

#include <json.h>

 

class facebook_client {

public:

       static facebook_client& instance(); // Singleton

       pplx::task<void> login(std::wstring scopes);

       pplx::task<web::json::value> get(std::wstring path);

       web::http::uri_builder base_uri(bool absolute = false);

 

private:

       facebook_client():

       raw_client(L“https://graph.facebook.com/”),

       signed_in(false) {}

 

       pplx::task<void> full_login(std::wstring scopes);

 

       std::wstring token_;

       bool signed_in;

       web::http::client::http_client raw_client;

};

Since we only need one instance of this class for our app, we are employing the singleton pattern and have made the constructor private.  This ensures that the client can be accessed from anywhere, and that multiple instances of the client do not proliferate through our app.

The following sections will walk through implementation of this class.  The code in the following sections assumes that your implementation file has these using declarations:

using namespace pplx;

using namespace web;

using namespace Platform;

using namespace Windows::Foundation;

using namespace Windows::Security::Authentication::Web;

using namespace Windows::Storage;

The implementation of the instance() method uses a Meyer’s Singleton:

facebook_client& facebook_client::instance()

{

       static facebook_client c;

       return c;

}

Logging into Facebook

Like many popular services, Facebook uses OAuth to perform user logins.  For Windows Store apps, developers should use the WebAuthenticationBroker to negotiate OAuth logins.  This API presents a trustworthy dialog for your users to consent to connecting your application to the authorizing service which is consistent across all Windows Store apps.  This dialog will guide your user through Facebook’s Desktop App Login.

The following code will present a dialog for the user to log into Facebook:

pplx::task<void> facebook_client::full_login(std::wstring scopes)

{

       http::uri_builder login_uri(L“https://www.facebook.com/dialog/oauth”);

       login_uri.append_query(L“client_id”, L“<appid>”); // App id

       login_uri.append_query(L“redirect_uri”, L“https://www.facebook.com/connect/login_success.html”);

       login_uri.append_query(L“scope”, scopes);

       login_uri.append_query(L“display”, L“popup”);

Posted in C++

0 comments

Discussion is closed.

Feedback usabilla icon