Skip to content

Commit

Permalink
身份验证 - 获取登录用户信息
Browse files Browse the repository at this point in the history
  • Loading branch information
foxzool committed Sep 23, 2024
1 parent a83ec0d commit 52e069e
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
APP_ID="app_id"
APP_SECRET="app_secret"
USER_ACCESS_TOKEN="test_user_access_token"
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,9 @@ path = "examples/api/bitable/v1/app_table_record/search.rs"
[[example]]
name = "app_table_field_list"
path = "examples/api/bitable/v1/app_table_field/list.rs"


[[example]]
name = "auth_get_user_info"
path = "examples/api/authen/v1/user_info.rs"

33 changes: 33 additions & 0 deletions examples/api/authen/v1/user_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use dotenvy::dotenv;
use std::env;

use open_lark::client::LarkClient;

/// 获取登录用户信息
#[tokio::main]
async fn main() {
dotenv().expect(".env file not found");
env_logger::init();
let app_id = env::var("APP_ID").unwrap();
let app_secret = env::var("APP_SECRET").unwrap();
let user_access_token = env::var("USER_ACCESS_TOKEN").unwrap();
// 创建 Client
let client = LarkClient::builder(&app_id, &app_secret).build();

// 发起请求
let resp = client
.auth
.v1
.user_info
.get(user_access_token)
.await
.unwrap();
match resp.data.as_ref() {
Some(data) => {
println!("user_info: {:#?}", data);
}
None => {
println!("user_info error: {:#?}", resp);
}
}
}
6 changes: 4 additions & 2 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Duration;
use crate::{
core::{config::Config, constants::AppType},
service::{
bitable::BitableService, drive::DriveService, im::ImService, search::SearchService,
sheets::SheetsService,
authentication::AuthenService, bitable::BitableService, drive::DriveService, im::ImService,
search::SearchService, sheets::SheetsService,
},
};

Expand All @@ -14,6 +14,7 @@ pub mod ws_client;
/// 飞书开放平台SDK client
pub struct LarkClient {
pub config: Config,
pub auth: AuthenService,
pub im: ImService,
pub drive: DriveService,
pub search: SearchService,
Expand Down Expand Up @@ -61,6 +62,7 @@ impl LarkClientBuilder {

LarkClient {
config: self.config.clone(),
auth: AuthenService::new(self.config.clone()),
im: ImService::new(self.config.clone()),
drive: DriveService::new(self.config.clone()),
search: SearchService::new(self.config.clone()),
Expand Down
2 changes: 1 addition & 1 deletion src/client/ws_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async fn get_conn_url(app_id: &str, app_secret: &str) -> WsClientResult<EndPoint
});

let req = reqwest::Client::new()
.post(&format!("{FEISHU_BASE_URL}/{END_POINT_URL}"))
.post(format!("{FEISHU_BASE_URL}/{END_POINT_URL}"))
.header("locale", "zh")
.json(&body)
.send()
Expand Down
13 changes: 13 additions & 0 deletions src/service/authentication/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub mod v1;

pub struct AuthenService {
pub v1: v1::V1,
}

impl AuthenService {
pub fn new(config: crate::core::config::Config) -> Self {
Self {
v1: v1::V1::new(config),
}
}
}
97 changes: 97 additions & 0 deletions src/service/authentication/v1/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use crate::core::api_req::ApiRequest;
use crate::core::api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat};
use crate::core::config::Config;
use crate::core::constants::AccessTokenType;
use crate::core::http::Transport;
use crate::core::req_option::RequestOption;
use crate::core::SDKResult;
use serde::Deserialize;

pub struct UserInfoService {
config: Config,
}

impl UserInfoService {
pub fn new(config: Config) -> Self {
Self { config }
}

/// 获取登录用户信息
pub async fn get(&self, user_access_token: impl ToString) -> SDKResult<BaseResponse<UserInfo>> {
let api_req = ApiRequest {
api_path: "/open-apis/authen/v1/user_info".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
..Default::default()
};

let option = RequestOption::builder()
.user_access_token(user_access_token)
.build();
let api_resp = Transport::request(api_req, &self.config, Some(option)).await?;

Ok(api_resp)
}
}

/// 登录用户信息
#[derive(Debug, Deserialize)]
pub struct UserInfo {
/// 用户姓名
pub name: String,
/// 用户英文名称
pub en_name: String,
/// 用户头像
pub avatar_url: String,
/// 用户头像 72x72
pub avatar_thumb: String,
/// 用户头像 240x240
pub avatar_middle: String,
/// 用户头像 640x640
pub avatar_big: String,
/// 用户在应用内的唯一标识
pub open_id: String,
/// 用户对ISV的唯一标识,对于同一个ISV,用户在其名下所有应用的union_id相同
pub union_id: String,
/// 用户邮箱
pub email: Option<String>,
/// 企业邮箱,请先确保已在管理后台启用飞书邮箱服务
pub enterprise_email: Option<String>,
/// 用户 user_id
pub user_id: String,
/// 用户手机号
pub mobile: Option<String>,
/// 当前企业标识
pub tenant_key: String,
/// 用户工号
pub employee_no: String,
}

impl ApiResponseTrait for UserInfo {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}

#[test]
fn test_user_info() {
let json_str = r#"{
"name": "zhangsan",
"en_name": "zhangsan",
"avatar_url": "www.feishu.cn/avatar/icon",
"avatar_thumb": "www.feishu.cn/avatar/icon_thumb",
"avatar_middle": "www.feishu.cn/avatar/icon_middle",
"avatar_big": "www.feishu.cn/avatar/icon_big",
"open_id": "ou-caecc734c2e3328a62489fe0648c4b98779515d3",
"union_id": "on-d89jhsdhjsajkda7828enjdj328ydhhw3u43yjhdj",
"email": "[email protected]",
"enterprise_email": "[email protected]",
"user_id": "5d9bdxxx",
"mobile": "+86130002883xx",
"tenant_key": "736588c92lxf175d",
"employee_no": "111222333"
}"#;

let user_info: UserInfo = serde_json::from_str(json_str).unwrap();

assert_eq!(user_info.name, "zhangsan")
}
16 changes: 16 additions & 0 deletions src/service/authentication/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pub use auth::*;

mod auth;

pub struct V1 {
/// 身份验证
pub user_info: UserInfoService,
}

impl V1 {
pub fn new(config: crate::core::config::Config) -> Self {
Self {
user_info: UserInfoService::new(config),
}
}
}
1 change: 1 addition & 0 deletions src/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod authentication;
pub mod bitable;
pub mod drive;
pub mod im;
Expand Down

0 comments on commit 52e069e

Please sign in to comment.