Graph API
A Helper Class
The examples on this page use a helper class, FBReturnObject
, to represent the data returned by a Graph API call.
#pragma once
namespace SampleCode
{
public ref class FBReturnObject sealed
{
public:
property Platform::String^ Id
{
Platform::String^ get()
{
return _id;
}
void set(Platform::String^ value)
{
_id = value;
}
}
property Platform::String^ Post_Id
{
Platform::String^ get()
{
return _post_id;
}
void set(Platform::String^ value)
{
_post_id = value;
}
}
private:
Platform::String^ _id;
Platform::String^ _post_id;
};
}
Post to Feed
The app should have publish_actions permission granted by the user.
The response will be an id of type string {"id": "<id here>"}
if published successfully.
You can find more details here.
#include "pch.h"
#include "FBReturnObject.h"
using namespace Windows::Foundation::Collections;
using namespace Platform;
using namespace concurrency;
using namespace winsdkfb;
using namespace winsdkfb::Graph;
namespace SampleCode
{
// Get active session
void GraphFeedPost()
{
FBSession^ sess = FBSession::ActiveSession;
if (sess->LoggedIn)
{
// Set caption, link and description parameters
PropertySet^ parameters = ref new PropertySet();
parameters->Insert(L"caption", L"Microsoft");
parameters->Insert(L"link", L"https://www.microsoft.com/en-us/default.aspx");
parameters->Insert(L"description", L"Microsoft home page");
// Add message
parameters->Insert(L"message", L"Posting from my Universal Windows app.");
//Create Graph API path
String^ graphPath = sess->User->Id + L"/feed";
// Create a json class factory with a class (FBReturnObject class)
// that can receive and parse the json response returned
FBJsonClassFactory^ fact = ref new FBJsonClassFactory([](String^ JsonText) ->
Object^
{
auto returnObject = ref new FBReturnObject();
returnObject->Id = Windows::Data::Json::JsonObject::Parse(JsonText)->GetNamedString("id");
return returnObject;
});
FBSingleValue^ sval = ref new FBSingleValue(graphPath, parameters, fact);
create_task(sval->PostAsync()).then([=](FBResult^ result)
{
if (result->Succeeded)
{
FBReturnObject^ response = static_cast<FBReturnObject ^>(result->Object);
}
else
{
// Posting failed
}
});
}
}
}
Custom Stories
Follow the steps here to configure custom stories in your app on developers.facebook.com
The app should have publish_actions permission granted by the user.
The response will be an id of type string {"id":"<id here>"}
if published successfully.
For the snippet below,
App namespace: fbsdk_sample_app
A custom story was created with action type try and object type scenario.
We define a custom json object parameter {"title": "Custom Story"}
and set the Graph API path to userId/fbsdk_sample_app:try
.
This will be published as “user tried scenario from Sample Application”.
#include "pch.h"
#include "FBReturnObject.h"
using namespace Windows::Foundation::Collections;
using namespace Platform;
using namespace concurrency;
using namespace winsdkfb;
using namespace winsdkfb::Graph;
namespace SampleCode
{
void CustomStory()
{
// Get active session
FBSession^ sess = FBSession::ActiveSession;
if (sess->LoggedIn)
{
// Create custom story with action:try and object:scenario
// This will be published to Facebook as :
// <user> tried <a scenario> from Sample Application
// Set parameters for custom story
PropertySet^ parameters = ref new PropertySet();
// Set object type parameter
// Object type: scenario
String^ customObjectInstance = "{" +
"\"type\":\"fbsdk_sample_app:scenario\"," + "\"title\":\"Custom Story\"" +
"}";
parameters->Insert("scenario", customObjectInstance);
// Get current user
FBUser^ user = sess->User;
// Set Graph api path for custom story (action:try)
String^ path = user->Id + L"/fbsdk_sample_app:try";
// Create a json class factory with a class (FBReturnObject class)
// that can receive and parse the json response returned
FBJsonClassFactory^ fact = ref new FBJsonClassFactory([](String^ JsonText) ->
Object^
{
auto returnObject = ref new FBReturnObject();
returnObject->Id = Windows::Data::Json::JsonObject::Parse(JsonText)->GetNamedString("id");
return returnObject;
});
FBSingleValue^ sval = ref new FBSingleValue(path, parameters, fact);
create_task(sval->PostAsync()).then([=](FBResult^ result)
{
if (result->Succeeded)
{
FBReturnObject^ response = static_cast<FBReturnObject ^>(result->Object);
}
else
{
// Posting failed
}
});
}
}
}
Upload a Photo
The app should have publish_actions permission granted by the user. The response will be an id of type string {"id": "<id here>"}
if published successfully.
You can find more details here.
#include "pch.h"
#include "FBReturnObject.h"
using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Windows::Storage::Pickers;
using namespace Platform;
using namespace concurrency;
using namespace winsdkfb;
using namespace winsdkfb::Graph;
namespace SampleCode
{
void UploadPhoto()
{
StorageFile^ selectedPhoto;
// Read image file into selectedPhoto
FileOpenPicker^ fop = ref new FileOpenPicker();
fop->ViewMode = PickerViewMode::Thumbnail;
fop->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
fop->FileTypeFilter->Append(L".jpg");
fop->FileTypeFilter->Append(L".png");
create_task(fop->PickSingleFileAsync()).then([&](StorageFile^ storageFile)
{
selectedPhoto = storageFile;
return storageFile->OpenReadAsync();
}).then([=, &selectedPhoto](IRandomAccessStreamWithContentType^ stream)
{
// Create media stream
FBMediaStream^ fbStream = ref new FBMediaStream(selectedPhoto->Name, stream);
// Get active session
FBSession^ sess = FBSession::ActiveSession;
if (sess->LoggedIn)
{
PropertySet^ parameters = ref new PropertySet();
// Set media stream
parameters->Insert("source", fbStream);
// Create Graph API path
String^ graphPath = sess->User->Id + L"/photos";
// Create a json class factory with a class (FBReturnObject class) that
// can receive and parse the json response returned
FBJsonClassFactory^ fact = ref new FBJsonClassFactory([](String^ JsonText) ->
Object^
{
auto returnObject = ref new FBReturnObject();
returnObject->Id = Windows::Data::Json::JsonObject::Parse(JsonText)->GetNamedString("id");
return returnObject;
});
FBSingleValue^ sval = ref new FBSingleValue(graphPath, parameters, fact);
create_task(sval->PostAsync()).then([=](FBResult^ result)
{
if (result->Succeeded)
{
FBReturnObject^ response = static_cast<FBReturnObject ^>(result->Object);
}
else
{
// Posting failed
}
});
}
});
}
}
Upload a Video (non-resumable)
The app should have publish_actions permission granted by the user. The response will be an id of type string {"id":"<id here>"}
if published successfully. Note that this is for uploading a small sized video all at once (non-resumable). The Facebook Graph API has said non-resumable upload supports videos up to 1GB and 20 minutes long.
#include "pch.h"
#include "FBReturnObject.h"
using namespace Windows::Storage;
using namespace Windows::Storage::Pickers;
using namespace Windows::Storage::Streams;
using namespace Windows::Data::Json;
using namespace Windows::Foundation::Collections;
using namespace concurrency;
using namespace Platform;
using namespace winsdkfb;
using namespace winsdkfb::Graph;
namespace SampleCode
{
void UploadVideo()
{
FileOpenPicker^ fop = ref new FileOpenPicker();
fop->ViewMode = PickerViewMode::Thumbnail;
fop->SuggestedStartLocation = PickerLocationId::VideosLibrary;
fop->FileTypeFilter->Append(L".mp4");
StorageFile^ storageFile;
create_task(fop->PickSingleFileAsync()).then([=, &storageFile](StorageFile^ tempStorageFile)
{
storageFile = tempStorageFile;
return tempStorageFile->OpenReadAsync();
}).then([=](IRandomAccessStreamWithContentType^ stream)
{
FBMediaStream^ mediaStream = ref new FBMediaStream(storageFile->Name, stream);
FBSession^ sess = FBSession::ActiveSession;
if (sess->LoggedIn)
{
FBUser^ user = sess->User;
PropertySet^ parameters = ref new PropertySet();
parameters->Insert(L"title", L"Test video");
parameters->Insert(L"source", mediaStream);
String^ path = L"/" + user->Id + L"/videos";
FBJsonClassFactory^ factory = ref new FBJsonClassFactory([](String^ s)
{
JsonObject^ jsonObject = JsonObject::Parse(s);
auto returnObject = ref new FBReturnObject();
returnObject->Id = jsonObject->GetNamedString(L"id");
returnObject->Post_Id = jsonObject->GetNamedString(L"post_id");
return returnObject;
});
FBSingleValue^ singleValue = ref new FBSingleValue(path, parameters, factory);
return create_task(singleValue->PostAsync());
}
return create_task([]()
{
return ref new FBResult(ref new FBError(0, L"Not logged in", L"Log in first"));
});
}).then([=](FBResult^ result)
{
if (result->Succeeded)
{
FBReturnObject^ response = static_cast<FBReturnObject^>(result->Object);
}
});
}
}
Like Action
The app should have publish_actions permission granted by the user. The response will be an id of type string {"id":"<id here>"}
if published successfully.
Note that this is not the same as ‘liking’ a Facebook Page. If successful, the like action will be published onto the user’s activity feed. You need extra permission to post it as an Open Graph object on the user’s timeline/news feed. You can find more details here.
#include "pch.h"
#include "FBReturnObject.h"
using namespace Windows::Foundation::Collections;
using namespace Platform;
using namespace concurrency;
using namespace winsdkfb;
using namespace winsdkfb::Graph;
namespace SampleCode
{
void LikeAction()
{
// Get active session
FBSession^ sess = FBSession::ActiveSession;
if (sess->LoggedIn)
{
// Set parameters
PropertySet^ parameters = ref new PropertySet();
// Set Uri to like
parameters->Insert("object", L"https://www.microsoft.com/en-us/default.aspx");
// Create Graph API path
String^ graphPath = sess->User->Id + L"/og.likes";
// Create a json class factory with a class (FBReturnObject class)
// that can receive and parse the json response returned
FBJsonClassFactory^ fact = ref new FBJsonClassFactory([](String^ JsonText) ->
Object^
{
auto returnObject = ref new FBReturnObject();
returnObject->Id = Windows::Data::Json::JsonObject::Parse(JsonText)->GetNamedString("id");
return returnObject;
});
FBSingleValue^ sval = ref new FBSingleValue(graphPath, parameters, fact);
create_task(sval->PostAsync()).then([=](FBResult^ result)
{
if (result->Succeeded)
{
FBReturnObject^ response = static_cast<FBReturnObject ^>(result->Object);
}
else
{
// Posting failed
}
});
}
}
}