Simple Setup & Usage
This is the simplest approach, but it requires that your Client ID
and Client Secret
be built into your application or
are somehow retrievable by your application at run-time.
While you can take steps to make extracting these from your project difficult (e.g. only storing encrypted strings which are
decrypted at run-time), there will always be a risk that one of your users will be able to locate and extract them.
Your Redirect URI
in your registered client should start with http://localhost
The process will work like this:
- You open the Patreon login and consent page for your user (either using their default web browser or the UMG Web Browser widget).
- While they are signing in and granting your application access to their data, we start up an HTTP server
a. By default the server will listen for a request at
http://localhost/login:8080
, but you can customize this as you see fit b. Whatever you configure the server to use, you need to ensure that your application's configuration on the Patreon side has a matchingRedirect URI
, e.g. if you setCallback Route
to/patreon
andCallback Port
to9999
, you must update the settings in Patreon to include aRedirect URI
ofhttp://localhost:9999/patreon
.The
Callback Route
MUST start with"/"
and be longer than just"/"
but can otherwise be anything you want.The
Callback Port
MUST be a positive integer, and unless you plan to use port80
should be larger than1024
- After consenting to sharing their data with your application, Patreon will make a request to the built-in HTTP server with a one-time use code we use to obtain an
Access Token
on behalf of the user. - Once we have an access token, we use this to request the user's account information, including any tiers and benefits they are entitled to.
- You can then use this information as you want, e.g. checking if they are pledged to a specific tier and unlocking some content for them.
That sounds like a lot, but steps 1-3 are encapsulated inside of a single function which can be chained to a second function to handle step 4 for you, leaving only the actual logic for your application in step 5 up to you to implement.
Blueprint
C++
#include "Actions/PerformAuthenticationAsyncAction.h"
#include "Actions/GetPatreonUserInfoAsyncAction.h"
#include "APITokenInfo.h"
#define CLIENT_ID TEXT("<Your Client ID>")
#define CLIENT_SECRET TEXT("<Your Client Secret>")
void MyClass::DoPatreonLogin() {
// create an authentication action expecting a callback at http://localhost/login:8080
UPerformAuthenticationAsyncAction* authAction = UPerformAuthenticationAsyncAction::PerformAuthenticationAsyncAction(
this, // world context
CLIENT_ID,
CLIENT_SECRET
// callback route defaults to "/login"
// callback port defaults to 8080
);
// register callbacks for results
authAction->OnComplete.AddDynamic(this, &MyClass::OnAuthenticationComplete);
authAction->OnFail.AddDynamic(this, &MyClass::OnAuthenticationFailed);
// kick off action
authAction->Activate();
}
void MyClass::OnAuthenticationComplete(FAPITokenInfo TokenInfo) {
// now that we have a token, we can use it to fetch information about the user who just logged in
UGetPatreonUserInfoAsyncAction* getInfoAction = UGetPatreonUserInfoAsyncAction::GetPatreonUserInfoAsyncAction(
this, // world context
TokenInfo
);
getInfoAction->OnComplete.AddDynamic(this, &MyClass::OnGetUserInfoComplete);
getInfoAction->OnFail.AddDynamic(this, &MyClass::OnGetUserInfoFail);
getInfoAction->Activate();
}
void MyClass::OnAuthenticationFailed() {
UE_LOG(LogTemp, Error,
TEXT("Patreon authentication failed: The user may have denied the request for their data")
);
}
void MyClass::OnGetUserInfoComplete(FPatreonUserInfo UserInfo) {
// now that we have the user's information,
// we can check if they have pledged to a tier with unlockable content
if (UserInfo.HasTier("Special Tier")) {
UnlockSecretConent();
} else {
// suggest the user should pledge to the Special Tier ;)
}
}
void MyClass::OnGetUserInfoFail() {
UE_LOG(LogTemp, Error,
TEXT("Fetching user info from Patreon API failed: Was the access token invalid?")
);
}
For any questions, help, suggestions or feature requests, please feel free to contact me at nbpsup@gmail.com