Browse Source

新增授权登陆方式创建提问箱

master
欧醚 3 years ago
parent
commit
b3d09b505f
2 changed files with 54 additions and 2 deletions
  1. +38
    -1
      ask.py
  2. +16
    -1
      static/ask.html

+ 38
- 1
ask.py View File

@ -7,11 +7,17 @@ import re, random, string, datetime
import html2text
BOT_NAME = '@ask_me_bot'
CLIENT_ID = 'WQHzKKvfahkkcFm_iErT6ZdYvczi8L6Uunsoa88bCKA'
CLIENT_SEC = open('client.secret', 'r').read().strip()
DOMAIN = 'thu.closed.social'
WORK_URL = 'https://closed.social'
#WORK_URL = 'http://127.0.0.1:5000'
REDIRECT_URI = WORK_URL + '/askMe/auth'
token = open('token.secret','r').read().strip('\n')
token = open('token.secret','r').read().strip()
th = Mastodon(
access_token = token,
api_base_url = 'https://' + DOMAIN
@ -79,6 +85,37 @@ def root():
def root_footer():
return app.send_static_file('footer.html')
@app.route('/askMe/auth')
@limiter.limit("10 / minute")
def set_inbox_auth():
code = request.args.get('code')
client = Mastodon(
client_id = CLIENT_ID,
client_secret = CLIENT_SEC,
api_base_url = 'https://' + DOMAIN
)
token = client.log_in(code=code, redirect_uri=REDIRECT_URI, scopes=['read', 'write'])
info = client.account_verify_credentials()
acct = info.acct
u = User.query.filter_by(acct=acct).first()
if not u:
u = User(acct)
u.secr = ''.join(random.choice(string.ascii_lowercase) for i in range(16))
db.session.add(u)
u.disp = info.display_name
u.url = info.url
u.avat = info.avatar
db.session.commit()
client.status_post(f"[自动发送] 我创建了一个匿名提问箱,欢迎提问~\n{WORK_URL}/askMe/{acct}/{u.secr}", visibility='public')
return redirect(f"/askMe/{acct}/{u.secr}")
@app.route('/askMe/inbox', methods=['POST'])
@limiter.limit("10 / minute")
def set_inbox():

+ 16
- 1
static/ask.html View File

@ -71,7 +71,9 @@
<hr style="border-top: 1px solid white;"/>
<div id="start" style="margin:30px auto;padding:15px;border: 1px dashed white;border-radius:15px; max-width:400px;">
<h4 id="start">选择任意一种方式创建提问箱</h4>
<div style="margin:30px auto;padding:15px;border: 1px dashed white;border-radius:15px; max-width:400px;">
<h5>私信bot认证</h5>
<p>输入闭社id并前往闭社私信 <b>@ask_me_bot</b> “新建”,然后点击下方按钮,即可新建或重置提问箱。
<a href="##" role="button"data-toggle="popover" >
<svg width="20px" viewBox="0 0 24 28"><path d="M15.07 11.25l-.9.92C13.45 12.89 13 13.5 13 15h-2v-.5c0-1.11.45-2.11 1.17-2.83l1.24-1.26c.37-.36.59-.86.59-1.41a2 2 0 0 0-2-2a2 2 0 0 0-2 2H8a4 4 0 0 1 4-4a4 4 0 0 1 4 4a3.2 3.2 0 0 1-.93 2.25M13 19h-2v-2h2M12 2A10 10 0 0 0 2 12a10 10 0 0 0 10 10a10 10 0 0 0 10-10c0-5.53-4.5-10-10-10z" fill="#626262"/></svg>
@ -92,6 +94,19 @@
</form>
</div>
<div style="margin:30px auto;padding:15px;border: 1px dashed white;border-radius:15px; max-width:400px;">
<h5>授权登陆认证</h5>
<p>不会重置已有的链接 自动发布一条公开嘟文</p>
<a id="auth" href="##" target="_blank" class="btn btn-primary mb-2">授权登陆</a>
<script>
var c_id = "WQHzKKvfahkkcFm_iErT6ZdYvczi8L6Uunsoa88bCKA";
var hostname = "thu.closed.social";
$('#auth').click(function() {
$(this).attr("href", `https://${hostname}/oauth/authorize?client_id=${c_id}&scope=read+write&&redirect_uri=${encodeURIComponent(location.origin+location.pathname+'auth')}&response_type=code`);
});
</script>
</div>
<hr/>

Loading…
Cancel
Save