fix: No such file or directory while auth fediverse accounts
This commit is contained in:
parent
6e50dc4364
commit
2384c6203c
1 changed files with 22 additions and 8 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
from aiogram.types import Message, CallbackQuery
|
from aiogram.types import Message, CallbackQuery
|
||||||
from aiogram.fsm.context import FSMContext
|
from aiogram.fsm.context import FSMContext
|
||||||
from aiogram.fsm.state import State, StatesGroup
|
from aiogram.fsm.state import State, StatesGroup
|
||||||
|
@ -11,6 +13,12 @@ router = Router()
|
||||||
class AuthStates(StatesGroup):
|
class AuthStates(StatesGroup):
|
||||||
waiting_for_token = State()
|
waiting_for_token = State()
|
||||||
|
|
||||||
|
def check_secrets_folder_exists() -> bool:
|
||||||
|
"""
|
||||||
|
检查 secrets 文件夹是否存在
|
||||||
|
"""
|
||||||
|
return os.path.exists('secrets') and os.path.isdir('secrets')
|
||||||
|
|
||||||
def check_client_cred_exists(instance: str) -> bool:
|
def check_client_cred_exists(instance: str) -> bool:
|
||||||
"""
|
"""
|
||||||
检查实例的凭据文件是否存在
|
检查实例的凭据文件是否存在
|
||||||
|
@ -52,13 +60,20 @@ async def handle_auth(message: Message, state: FSMContext):
|
||||||
await message.reply('请输入实例域名,例如:`example.com`')
|
await message.reply('请输入实例域名,例如:`example.com`')
|
||||||
return
|
return
|
||||||
if not check_client_cred_exists(instance):
|
if not check_client_cred_exists(instance):
|
||||||
|
try:
|
||||||
|
if not check_secrets_folder_exists():
|
||||||
|
os.mkdir('secrets')
|
||||||
Mastodon.create_app(
|
Mastodon.create_app(
|
||||||
'realbot',
|
'realbot',
|
||||||
api_base_url='https://{}'.format(instance),
|
api_base_url='https://{}'.format(instance),
|
||||||
to_file='secrets/realbot_{}_clientcred.secret'.format(instance),
|
to_file='secrets/realbot_{}_clientcred.secret'.format(instance),
|
||||||
scopes=['read:accounts', 'read:statuses','write:media','write:statuses']
|
scopes=['read:accounts', 'read:statuses','write:media','write:statuses']
|
||||||
)
|
)
|
||||||
mastodon = Mastodon(client_id=f'secrets/realbot_{instance}_clientcred.secret', )
|
except Exception as e:
|
||||||
|
logging.warning(e)
|
||||||
|
await message.reply(f'创建应用失败:{str(e)}\n请确保实例域名正确并且实例支持 Mastodon API。')
|
||||||
|
return
|
||||||
|
mastodon = Mastodon(client_id=f'secrets/realbot_{instance}_clientcred.secret')
|
||||||
auth_url = mastodon.auth_request_url()
|
auth_url = mastodon.auth_request_url()
|
||||||
await message.reply('请在浏览器中打开链接进行身份验证:\n{}\n验证完成后,请用得到的 token 回复这条消息'.format(auth_url))
|
await message.reply('请在浏览器中打开链接进行身份验证:\n{}\n验证完成后,请用得到的 token 回复这条消息'.format(auth_url))
|
||||||
# 在发送消息后设置状态
|
# 在发送消息后设置状态
|
||||||
|
@ -90,7 +105,6 @@ async def handle_post_to_fedi(message: Message):
|
||||||
user_id = message.from_user.id
|
user_id = message.from_user.id
|
||||||
|
|
||||||
# 查找用户绑定的实例
|
# 查找用户绑定的实例
|
||||||
import os
|
|
||||||
import glob
|
import glob
|
||||||
|
|
||||||
user_cred_pattern = f'secrets/realbot_*_{user_id}_usercred.secret'
|
user_cred_pattern = f'secrets/realbot_*_{user_id}_usercred.secret'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue