Skip to content

Commit ba6f14f

Browse files
authored
Merge pull request #10 from pelican-plugins/dev
OAuth token authentication
2 parents a9078eb + ff63981 commit ba6f14f

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ In order to publish on Mastodon you need to enter in `.env` file, on the site ro
4242

4343
``` python
4444
MASTODON_BASE_URL="URL of your Mastodon instance. For example https://mastodon.social"
45-
MASTODON_USERNAME="Your username for Mastodon login"
46-
MASTODON_PASSWORD="You password for Mastodon login"
45+
MASTODON_OAUTH_TOKEN="Token of your Mastodon registered App"
4746
```
4847

48+
:exclamation: Starting from Mastodon 4.4.0 posting using Username/Password has been deprecated. Now you have to use the OAuth Token method instead. Your OAuth Token is available in the admin page of your web Mastodon page.
49+
4950
There is no need to register an app in your Mastodon profile because *Fediverse* will do it for you!
5051

5152
On every run *Fediverse* looks for a file called `pelicanfediverse_clientcred.secret` and - if it is not found - it gets in touch with Mastodon, creates an app called *PelicanFediverse* and writes API keys and other necessary information in this file.
@@ -59,9 +60,10 @@ In `pelicanconf.py` some new parameters can be defined
5960

6061
- **MASTODON_VISIBILITY** : Set post's visiblity on mastodon. Can be 'direct', 'private', 'unlisted' or 'public'. Default value = 'direct'
6162

62-
More details : https://mastodonpy.readthedocs.io/en/stable/05_statuses.html#writing
63+
More details : https://mastodonpy.readthedocs.io/en/stable/05_statuses.html#writing
64+
6365
- **MASTODON_READ_MORE** : Text to add at the end of the post, before link to the pelican's article. Default value = 'Read more: '
6466

6567
``` Python
6668
MASTODON_VISIBILITY='direct'
67-
MASTODON_READ_MORE='Read more: '
69+
MASTODON_READ_MORE='Read more: '

fediverse.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,42 @@ def write_articleslist(articleslist):
3737
def post_on_mastodon(settings, new_posts):
3838

3939
load_dotenv()
40+
# Load admin details
4041
global mt_base_url
4142
mt_base_url = os.getenv('MASTODON_BASE_URL')
4243
global mt_username
43-
mt_username = os.getenv('MASTODON_USERNAME')
44+
mt_username = os.getenv('MASTODON_USERNAME') # DEPRECATED: to be removed soon
4445
global mt_password
45-
mt_password = os.getenv('MASTODON_PASSWORD')
46-
46+
mt_password = os.getenv('MASTODON_PASSWORD') # DEPRECATED to be removed soon
47+
global mt_token
48+
mt_token = os.getenv('MASTODON_OAUTH_TOKEN')
49+
# Load other details
4750
global mt_read_more
4851
mt_read_more = settings.get('MASTODON_READ_MORE', 'Read more: ')
4952
global mt_visibility
5053
mt_visibility = settings.get('MASTODON_VISIBILITY', 'direct')
5154

52-
# check if config file has been duly filled or print an error message and exit
53-
if mt_base_url == '' or mt_username == '' or mt_password == '':
54-
logger.warning('Pelican_fediverse: Mastodon access credentials not configured...')
55+
# check if config file contains username and password and prompt the user this is deprecated
56+
if mt_username or mt_password in globals():
57+
logger.warning('Pelican_fediverse: password authentication is DEPRECATED and will be removed soon!\nPlease use OAuth token instead...')
58+
59+
# check if config file contains OAuth token and exit if not
60+
if mt_base_url == '' or mt_token == '':
61+
logger.warning('Pelican_fediverse: Mastodon instance URL and/or OAuth token are missing!\nPlease check your config file...')
5562
sys.exit(9)
5663

57-
# if pelicantoot_clientcred.secret does not exist it means we have to create the app on Mastodon
64+
# if pelicantoot_clientcred.secret does not exist it means we have to create our Mastodon app
5865
if os.path.exists('pelicanfediverse_clientcred.secret') == False:
5966
Mastodon.create_app(
6067
'PelicanFediverse',
6168
api_base_url = mt_base_url,
6269
to_file = 'pelicanfediverse_clientcred.secret'
6370
)
6471

65-
# Advise the user with an on-screen message. We are ready to publish!
66-
build_message = 'Publishing on Mastodon: %s (%s)'
72+
# Prepare pubish a message for the user
73+
build_message = 'Articles found to publish on Mastodon: %s (%s)'
6774

75+
# Collect/print information for articles
6876
for article in new_posts:
6977
url = article.get_siteurl() + article.url
7078
title = article.title
@@ -84,24 +92,17 @@ def post_updates(generator, writer):
8492
if article.url not in articleslist:
8593
new_posts.append(article)
8694

87-
# we only write the newly found sites to disk if posting them worked. that way we can retry later
95+
# We only write the newly found sites to disk if posting them worked. That way we can retry later
8896
if new_posts:
8997
if post_on_mastodon(generator.settings, new_posts):
98+
# Log in
9099
mastodon = Mastodon(
91100
client_id = 'pelicanfediverse_clientcred.secret',
101+
access_token = mt_token,
92102
api_base_url = mt_base_url
93103
)
94-
mastodon.log_in(
95-
mt_username,
96-
mt_password,
97-
to_file = 'pelicanfediverse_usercred.secret'
98-
)
99-
mastodon = Mastodon(
100-
access_token = 'pelicanfediverse_usercred.secret',
101-
api_base_url = mt_base_url
102-
)
103-
# Actually build the post structure
104-
# First set a maximum length for the final post
104+
# Build the post structure
105+
# First, set a maximum length for the final post
105106
toot_maxlength = 490 # Actually 500 but let's keep a safety gap for miscalculation...
106107
for article in new_posts:
107108
articleslist.append(article.url)

0 commit comments

Comments
 (0)