Implement Bilibili dynamic posting API endpoint with JWT authentication and streamlined error handling#3
Merged
daflyinbed merged 29 commits intomainfrom Jan 10, 2026
Conversation
Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Implement API for Bilibili dynamic and image upload
Implement Bilibili dynamic posting API endpoint
Jan 7, 2026
Collaborator
|
- Change route from /api/v1 to /api - Replace API key auth with JWT authentication using ES256 - Add JWT configuration with public/private keys - Create reusable JWT middleware - Add CLI command to generate JWT tokens - Remove api_key from Bilibili config - Update example.toml with JWT configuration Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Contributor
Author
All changes implemented in commit 68a890f:
The JWT middleware is now applied to all Bilibili endpoints. Generate ES256 keys using: openssl ecparam -genkey -name prime256v1 -noout -out private.pem
openssl ec -in private.pem -pubout -out public.pem |
- Update API endpoint path from /api/v1 to /api - Replace API key examples with JWT Bearer token auth - Add JWT token generation instructions - Update error messages for JWT auth - Update code examples for JavaScript and Python - Fix OpenAPI documentation URLs Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Copilot
AI
changed the title
Implement Bilibili dynamic posting API endpoint
Implement Bilibili dynamic posting API endpoint with JWT authentication
Jan 8, 2026
Collaborator
|
- Remove Option wrapper from jwt field in AppSettings (now required)
- Remove Option wrapper from jwt_config in AppState
- Simplify auth error responses to only return {code: 1}
- Log detailed error messages instead of including in response
- Update example.toml to reflect JWT as required config
Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Contributor
Author
Changes implemented in commit 944698e:
Example error response: {
"code": 1
}Logs contain specific details like "JWT verification failed: ExpiredSignature" for debugging. |
daflyinbed
reviewed
Jan 8, 2026
Collaborator
|
@copilot jwt的claims改成用RegisteredClaims 去掉exp 只留iat 和 sub |
- Combine health and bilibili routes into one OpenApiRouter - Use route_layer to apply JWT middleware only to routes added after it - Health endpoints remain public (no auth) - Bilibili endpoints protected by JWT (added after route_layer) - Eliminates need for separate router creation and merge Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
- Re-add Unauthorized variant to AppError enum - Maps to HTTP 401 UNAUTHORIZED status code - Stores anyhow::Error for rich error context - Can be used for authentication/authorization errors - Now have 3 variants: BadRequest (400), Unauthorized (401), InternalError (500) Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Collaborator
|
@copilot 重构src/auth.rs 用上AppError::Unauthorized |
- Replace manual error responses with AppError::Unauthorized - Remove unused imports (Json, StatusCode, serde_json, warn) - Use anyhow::anyhow! to create error messages with context - Return Result<Response, AppError> from jwt_auth_middleware - Simplify error handling with ? operator - All authentication errors now go through AppError's IntoResponse - Consistent error logging and response format via AppError Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Contributor
Author
Refactored in commit 2d813e2:
|
daflyinbed
reviewed
Jan 9, 2026
- Change return type from Result<Response, AppError> to AppResult<Response> - Import AppResult from error module - Consistent with other handler return types across the application Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 13 changed files in this pull request and generated 9 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Add create_dynamic_with_scene helper function - Reduces code duplication between scene 1 (text-only) and scene 2 (with images) - Maintains same functionality while improving maintainability - Reduces bilibili_handlers.rs by ~60 lines - Addresses code review feedback about DRY principle Co-authored-by: daflyinbed <21363956+daflyinbed@users.noreply.github.com>
Collaborator
|
Close #2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implementation Plan for Bilibili Dynamic Posting API
Completed
Recent Changes (Addressing PR Comments)
/api/v1/createDynamicto/api/createDynamicgenerate-jwtto generate JWT tokens{code: 1}with loggingSummary
Successfully implemented Bilibili dynamic posting API with JWT authentication and streamlined error handling:
/api/createDynamiccargo run -- generate-jwt --config config.toml --subject <user_id>{code: 1}via AppErrorAuthentication
AppError::Unauthorizedand return{code: 1}subandiatclaims (no expiration)Router Architecture
Code Quality
Error Handling
BadRequest(400),Unauthorized(401),InternalError(500)anyhow::Errorinternally for rich error context and full error chainsFromtrait implementations{code: 1}(no msg field exposed to clients)anyhow::Contextfor adding contextual information to error chains?operator for cleaner code flow and better error propagationOriginal prompt
This section details on the original issue you should resolve
<issue_title>实现一个接口:支持如 Node.js 代码所示的 Bilibili 动态/图片动态发布</issue_title>
<issue_description>## 需求描述
需要在本项目中实现一个接口,功能与以下 Node.js 代码(express + multer + got)完全一致:可将文本与图片上传至 Bilibili 动态。
功能参考代码:
// 见下方 nodejs 代码实现Node.js实现参考