我正在构建一个Tauri应用程序,并希望建立与谷歌的OAuth集成。为此,我将需要一个用于oauth回调的URI,但Tauri不清楚如何配置模式(可能使用这方法或使用WindowUrl )
我如何向我的Tauri应用程序添加一个URI,这样我就可以像下面的例子那样喜欢它了:myapp://callback
我认为它看起来可能是这样的:
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.register_uri_scheme_protocol("myapp", move |app, request| {
# protocol logic here
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}发布于 2022-12-02 09:20:17
Tauri目前并不直接支持深度链接。我发现的一个很好的替代方案是这锈病项目。安装之后,您可以执行如下操作:
#[tauri::command]
async fn start_oauth_server(window: Window) -> Result<u16, String> {
println!("Starting server");
start(None, move |url| {
// Because of the unprotected localhost port, you must verify the URL here.
// Preferebly send back only the token, or nothing at all if you can handle everything else in Rust.
// convert the string to a url
let url = url::Url::parse(&url).unwrap();
// get the code query parameter
let code = url
.query_pairs()
.find(|(k, _)| k == "code")
.unwrap_or_default()
.1;
// get the state query parameter
let state = url
.query_pairs()
.find(|(k, _)| k == "state")
.unwrap_or_default()
.1;
// create map of query parameters
let mut query_params = HashMap::new();
query_params.insert("code".to_string(), code.to_string());
query_params.insert("state".to_string(), state.to_string());
query_params.insert(String::from("redirect_uri"), url.to_string());
if window.emit("redirect_uri", query_params).is_ok() {
println!("Sent redirect_uri event");
} else {
println!("Failed to send redirect_uri event");
}
})
.map_err(|err| err.to_string())
}https://stackoverflow.com/questions/74597101
复制相似问题