修改授权,如果是同一个登录账号,任意授权都会更新全部token
This commit is contained in:
@@ -89,7 +89,6 @@ class DouyinRequest:
|
||||
if token:
|
||||
return token
|
||||
|
||||
|
||||
async with async_session() as db:
|
||||
oauth_data = await db.execute(
|
||||
select(UserOAuth).where(
|
||||
@@ -103,17 +102,30 @@ class DouyinRequest:
|
||||
raise ValueError("无效的oauth_id")
|
||||
|
||||
if force_refresh:
|
||||
where_cond = UserOAuth.deleted_at.is_(None)
|
||||
if oauth_data.appid:
|
||||
where_cond = where_cond & (UserOAuth.appid == oauth_data.appid)
|
||||
if oauth_data.account_username:
|
||||
where_cond = where_cond & (UserOAuth.account_username == oauth_data.account_username)
|
||||
if oauth_data.account_userid:
|
||||
where_cond = where_cond & (UserOAuth.account_userid == oauth_data.account_userid)
|
||||
|
||||
await db.execute(
|
||||
update(UserOAuth).where(UserOAuth.id == oauth_id).values(
|
||||
update(UserOAuth).where(where_cond).values(
|
||||
access_token=None,
|
||||
access_token_expired=None,
|
||||
)
|
||||
)
|
||||
await db.commit()
|
||||
await self._delete_redis_token(oauth_id)
|
||||
|
||||
related_oauth_ids = await db.execute(
|
||||
select(UserOAuth.id).where(where_cond)
|
||||
)
|
||||
related_oauth_ids = [row[0] for row in related_oauth_ids.all()]
|
||||
for related_id in related_oauth_ids:
|
||||
await self._delete_redis_token(related_id)
|
||||
|
||||
new_token, new_expired_at = await self.refresh_access_token(db, oauth_id, oauth_data.appid, oauth_data.refresh_token)
|
||||
await self._set_redis_token(oauth_id, new_token, new_expired_at)
|
||||
return new_token
|
||||
|
||||
if oauth_data.access_token_expired and oauth_data.access_token_expired > datetime.now(timezone.utc):
|
||||
@@ -122,14 +134,46 @@ class DouyinRequest:
|
||||
await self._set_redis_token(oauth_id, token, expired_at)
|
||||
return token
|
||||
|
||||
where_cond = UserOAuth.deleted_at.is_(None)
|
||||
if oauth_data.appid:
|
||||
where_cond = where_cond & (UserOAuth.appid == oauth_data.appid)
|
||||
if oauth_data.account_username:
|
||||
where_cond = where_cond & (UserOAuth.account_username == oauth_data.account_username)
|
||||
if oauth_data.account_userid:
|
||||
where_cond = where_cond & (UserOAuth.account_userid == oauth_data.account_userid)
|
||||
|
||||
related_oauths = await db.execute(
|
||||
select(UserOAuth.access_token, UserOAuth.access_token_expired).where(
|
||||
where_cond,
|
||||
UserOAuth.access_token_expired.is_not(None),
|
||||
UserOAuth.access_token_expired > datetime.now(timezone.utc),
|
||||
).limit(1)
|
||||
)
|
||||
related_oauth = related_oauths.first()
|
||||
if related_oauth:
|
||||
token, expired_at = related_oauth
|
||||
await self._set_redis_token(oauth_id, token, expired_at)
|
||||
return token
|
||||
|
||||
if oauth_data.refresh_token_expired and oauth_data.refresh_token_expired < datetime.now(timezone.utc):
|
||||
raise ValueError("授权已过期,请重新授权")
|
||||
|
||||
new_token, new_expired_at = await self.refresh_access_token(db, oauth_id, oauth_data.appid, oauth_data.refresh_token)
|
||||
await self._set_redis_token(oauth_id, new_token, new_expired_at)
|
||||
return new_token
|
||||
|
||||
async def refresh_access_token(self, db: AsyncSession, oauth_id: str, appid: str, refresh_token: str) -> Tuple[str, datetime]:
|
||||
oauth_info = await db.execute(
|
||||
select(UserOAuth.account_username, UserOAuth.account_userid).where(
|
||||
UserOAuth.id == oauth_id,
|
||||
UserOAuth.deleted_at.is_(None),
|
||||
).limit(1)
|
||||
)
|
||||
oauth_info = oauth_info.first()
|
||||
if not oauth_info:
|
||||
raise ValueError("无效的oauth_id")
|
||||
|
||||
account_username, account_userid = oauth_info
|
||||
|
||||
result = await db.execute(
|
||||
select(UserOAuthApp.secret).where(
|
||||
UserOAuthApp.app_id == appid,
|
||||
@@ -169,9 +213,17 @@ class DouyinRequest:
|
||||
|
||||
new_expired_at = datetime.now(timezone.utc) + timedelta(seconds=expires_in)
|
||||
|
||||
where_cond = UserOAuth.deleted_at.is_(None)
|
||||
if appid:
|
||||
where_cond = where_cond & (UserOAuth.appid == appid)
|
||||
if account_username:
|
||||
where_cond = where_cond & (UserOAuth.account_username == account_username)
|
||||
if account_userid:
|
||||
where_cond = where_cond & (UserOAuth.account_userid == account_userid)
|
||||
|
||||
await db.execute(
|
||||
update(UserOAuth).where(
|
||||
UserOAuth.id == oauth_id,
|
||||
where_cond,
|
||||
).values(
|
||||
access_token=new_access_token,
|
||||
refresh_token=new_refresh_token,
|
||||
@@ -181,6 +233,14 @@ class DouyinRequest:
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
related_oauth_ids = await db.execute(
|
||||
select(UserOAuth.id).where(where_cond)
|
||||
)
|
||||
related_oauth_ids = [row[0] for row in related_oauth_ids.all()]
|
||||
|
||||
for related_id in related_oauth_ids:
|
||||
await self._set_redis_token(related_id, new_access_token, new_expired_at)
|
||||
|
||||
return new_access_token, new_expired_at
|
||||
|
||||
# 有token请求
|
||||
@@ -267,19 +327,6 @@ class DouyinRequest:
|
||||
else:
|
||||
raise ValueError('网络错误,稍后重试。')
|
||||
|
||||
# options_log = {}
|
||||
# if options:
|
||||
# for key, value in options.items():
|
||||
# if key == 'files':
|
||||
# options_log[key] = {k: (v[0], 'bytes_content', v[2]) for k, v in value.items()}
|
||||
# else:
|
||||
# options_log[key] = value
|
||||
# raise RuntimeError(
|
||||
# f'DouYin API request failed after 5 retries. '
|
||||
# f'url:{url};oauthId:{oauth_id};options:{json.dumps(options_log)};response:{res}'
|
||||
# )
|
||||
# if code != 0:
|
||||
# raise ValueError(f'response:{res}')
|
||||
|
||||
# 无token请求
|
||||
async def request_with_context(
|
||||
|
||||
Reference in New Issue
Block a user