Patreon auth in Ren'Py
A downloadable asset pack
Disclaimer
Only use this if you know what you are doing. With Client Secret anyone could get access to your campaign info, all of your patrons' profile info, email addresses, and their pledge amounts
For production, it would be better to have a dedicated server to handle authorisation.
Setup
First, you need to create a client on Patreon.
Go to the https://www.patreon.com/portal/registration/register-clients and click on the "Create Client" button.
- App Name — enter your game name
- Description — enter some text. It will displayed to the user on authentication page.
- App Category — Select something
- Redirect URIs — enter http://localhost:6167/auth
- Client API Version — should be 2
- Other fields are not required.
You only need 2 fields "Client ID" and "Client Secret" from the newly created client.
Somewhere in your game (for example, in the "options.rpy") write this code:
define PATREON_CLIENT_ID = "<YOUR_CLIENT_ID>"
define PATREON_CLIENT_SECRET = "<YOUR_CLIENT_SECRET>"
Replace the "<YOUR_CLIENT_ID>" with your Client ID value and
"<YOUR_CLIENT_SECRET>" with your Client Secret value.
From the archive that you downloaded from itch, you'll need 4 files inside the "patreon_auth" folder. Extract this folder inside your game folder (i.e ("MyGame/game/patreon_auth")
Now somewhere in the game you can use this action to start auth process:
PatreonAuth(PATREON_CLIENT_ID, PATREON_CLIENT_SECRET, "/auth")
For example:
textbutton _("Auth with Patreon") action PatreonAuth(PATREON_CLIENT_ID, PATREON_CLIENT_SECRET, "/auth")
By default, user info are saved inside persistent.PATREON_USER_DATA
variable.
For example, to get user name:
persistent.PATREON_USER_DATA.get("data").get("attributes").get("full_name")
More info about other fields here: https://docs.patreon.com/#user-v2.
Scopes
Some info are not sent from Patreon by default. To get it, you need to edit scopes inside the "patreon_auth/PatreonAuth.rpy" file.
For example, to get user email, update the scope parameter to this:
scope = "identity identity[email]"
.
Also, you need to update requested fields inside the on_success_auth
function. Add "email" to the "fields[user]" field:
user = client.get_user_data({"fields[user]": "full_name,image_url,email"...
More info about scopes here https://docs.patreon.com/#scopes
How to tell if a player is subscribed
Something like this should work:
included = persistent.PATREON_USER_DATA.get("included") if len(included) > 0: attributes = included[0].get("attributes", {}) patron_status = attributes.get("patron_status") amount = attributes.get("currently_entitled_amount_cents")
patron_status
variable could be one of those: active_patron
, declined_patron
, former_patron
or None (if user never pledged).
More info here: https://docs.patreon.com/#member
Update user info
To update user info, you can use something like this (not tested):
client = PatreonClient(persistent.PATREON_TOKENS.get("access_token")) # fields may vary, depending on your needs user = client.get_user_data({"fields[user]": "full_name,image_url", "include": "memberships", "fields[member]": "patron_status,currently_entitled_amount_cents"}) persistent.PATREON_USER_DATA = user renpy.restart_interaction()
Expiration
After a month Patreon tokens will expire and you won't be able to update user info.
You'll need to either manually refresh the tokens (see https://docs.patreon.com/#step-7-keeping-up-to-date) or ask the player to sign in via Patreon again.
Updated | 20 days ago |
Status | Released |
Category | Assets |
Release date | Nov 17, 2023 |
Rating | Rated 5.0 out of 5 stars (3 total ratings) |
Author | Dinaki |
Made with | Ren'Py |
Tags | authentication, patreon, Ren'Py, script |
Code license | MIT License |
Download
Click download now to get access to the following files:
Comments
Log in with itch.io to leave a comment.
This just screams security/privacy issue. If you put your client ID and secret in Ren'Py code, which anyone can go in and read, they'd have access to your client and would potentially be able to retrieve all your Patreon data, including names and email addresses.
Yeah, you right. I didn't think someone could use that in the final public build. I added a warning at the beginning.
Hi, your code is awesome, but whenever I log in to patreon and then try to save the game I get an error:
```
I'm sorry, but an uncaught exception occurred.
While running game code:
File "renpy/common/00gamemenu.rpy", line 174, in script
$ ui.interact()
File "renpy/common/00gamemenu.rpy", line 174, in <module>
$ ui.interact()
File "renpy/common/00action_file.rpy", line 397, in __call__
renpy.save(fn, extra_info=save_name)
TypeError: cannot pickle '_thread.lock' object (perhaps store.webserver.server._BaseServer__is_shut_down._cond._lock = <unlocked _thread.lock object at 0x00000000066a4600>)
I managed to put a band aid on this by making renpy restart after each authentication, I saw you had them in your code, but for some reason it did not do it on my laptop so in the end that's fine.
Also it does not work on mobile, the patreon link shows up and then the page loads for a while and then crashes.
Also it crashes directly in the browser:
I've uploaded a new version that fixes the first problem with saving.
I'll look into the second problem when I have more time.
Hello, I have a problem. Observe the following error when clicking the "Auth With Patreon" button This page is not working No data was sent by localhost ERR_EMPTY_RESPONSE
"http://localhost:6167/auth?code=08DDd0e75udOHwbpHrJorzUF8Hdtme&state=None"
Hello. Have you replaced `PATREON_CLIENT_ID` and `PATREON_CLIENT_SECRET` in the "options.rpy" file?
There is no `APP_NAME would like to...` window?
Yes, I changed it to
Patreon authentication
set PATREON_CLIENT_ID = "93VC-W99QouZubcHXV9F76GchkNzIjyHwaYppU3hQX9CN2ZrQTPhuUTsm7UIsGiY"
set PATREON_CLIENT_SECRET = "MC2CJKrpErv96aVhOHJTH54bmSMta5YonMqFJoRGG4aqiSTnSNqR5p_vpfltql2v"
In Options.py
Everything works normally until the
View your public profile (Allow) (Deny)
When clicking Allow, the error message appears on the web page.
[This page is not workinglocalhost did not send any data.
ERR_EMPTY_RESPONSE]
The problem appears after the `APP_NAME would like to...` window
Did you change anything else?
I just replaced ID and SECRET in options.rpy in the default project and got a successful result.
Possible solution:
In the patreon_auth/OAuth2Strategy.rpy replace this line:
With this:
And in the patreon client replace Redirect URIs with http://127.0.0.1:6167/auth
{"error":"invalid_request","error_description":"Mismatching redirect URI."}
Now this error appears when clicking (Allow)
I didn't modify anything, I used the file you released here. And I put the pateron credentials. Is there a problem with the files you imported and for some reason I don't have them?
(import http.server
from urllib.parse import urlparse, parse_qs, urlencode
from urllib.request import urlopen, Request
import threading
import json)
I managed to resolve this error mentioned above, but now the screen has the same error of not loading again.
Thanks for sharing the code!
Do you plan to publish this code on github?
What is the usage license of your tool?
Hello. No plans to publish on github for now.
MIT License.