Hi
I'm trying to login to a service and get a token. The code as such seems to work and I can print the response which is in json. So I want to deserialize the json response, so I can work on getting to the actual data But for some reason I cannot figure out how to make it work. Can someone help me make it work?
I get the following error in the current attempt
Rocket has launched from http://127.0.0.1:8000
GET /test?code=fcd960f6-4dcb-4223-bcd5-14304cc917ff&state=ramdom text/html:
>> Matched: (callback) GET /test?<code>&<state>
Err(
Error("invalid type: map, expected a string", line: 1, column: 0),
)
>> Outcome: Success
>> Response succeeded.
I'm still learning rust, but I have hit the wall and don't know how to make it work.
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
use rocket::response::Redirect;
extern crate reqwest;
use std::collections::HashMap;
extern crate serde;
use serde::{Deserialize, Serialize};
extern crate serde_json;
use serde_json::{Result, Value, Error};
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct JsonResponse {
access_token: String,
token_type: String,
expires_in: u16,
refresh_token: String,
refresh_token_expires_in: u16,
base_uri: Option,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
enum Option {
Some(String),
None,
}
/*{
"access_token": "eyJhbGciOiJFUzI1NiIsIng1dCI6IjhGQzE5Qjc0MzFCNjNFNTVCNjc0M0QwQTc5MjMzNjZCREZGOEI4NTAifQ.eyJvYWEiOiIzMzMzMCIsImlzcyI6Im9hIiwiYWlkIjoiMTg2NyIsInVpZCI6IkMxdkhaWThNZjg3cFNQVHNuOHJZSnc9PSIsImNpZCI6IkMxdkhaWThNZjg3cFNQVHNuOHJZSnc9PSIsImlzYSI6IkZhbHNlIiwidGlkIjoiNjg5MiIsInNpZCI6IjhkYjFjYWY1NDhhODQyNTVhNGNhMTQ5NDFjMzBlODg5IiwiZGdpIjoiODQiLCJleHAiOiIxNjI1NTEzODMyIiwib2FsIjoiMUYifQ.SkdlScwDoS5AagtUk3A9eyIu-wKLkzZ9g1i1iWPUGJSAyiMDMtRcp02gK-6UMmK3EBZY04MW3M-GFi-P_n1wsw",
"token_type": "Bearer",
"expires_in": 1200,
"refresh_token": "5b6e6987-82bd-46cb-9e08-5675fa7614ff",
"refresh_token_expires_in": 3600,
"base_uri": null
}*/
use std::future::Future;
use std::option;
// https://sim.logonvalidation.net/authorize?response_type=code&client_id=ZYX&state=y90dsygas98dygoidsahf8sa&redirect_uri=http://localhost/test
#[get("/login")]
fn login() -> Redirect {
let client_id = "ZYX";
let redirect_uri = "http://localhost:8000/test";
let response_type = "code";
let state = "ramdom";
let uri = "url";
Redirect::to(format!(
"{}authorize?response_type={}&Client_id={}&state={}&redirect_uri={}",
uri, response_type, client_id, state, redirect_uri,
))
}
//http://localhost:8000/test?code=0286b802-1aab-431c-a17d-e44f4a309876&state=ramdom
#[get("/test?<code>&<state>")]
async fn callback(code: String, state: String) -> () {
let uri = "url";
let mut map = HashMap::new();
map.insert("grant_type", "authorization_code");
map.insert("code", &code);
//map.insert("Content-Type", "application/x-www-form-urlencoded");
//map.insert("redirect_uri", "http://localhost:8000/final");
let client = reqwest::Client::new();
let res = client.post(uri)
.basic_auth("XYZ", Some("XYZ"))
.json(&map)
.send()
.await;
//println!("{:#?}", res.unwrap().text().await.unwrap());
//println!("{:#?}", res.unwrap().text().await.unwrap());
//let s: String = res.unwrap().text().await.unwrap().to_owned();
//let s_slice: &str = &s[..]; // take a full slice of the string
let profiles = serde_json::Deserializer::from_str(&res.unwrap().text().await.unwrap());
println!( "{:?}", profiles);
//let mut my_json: String = String::from(&res.unwrap().text().await.unwrap());
//println!( "{:#?}", serde_json::from_str::<String>(&res.unwrap().text().await.unwrap()));
//let data: JsonResponse = serde_json::from_str(s_slice).unwrap();
//println!( "{:#?}", data.unwrap());
//format!( "{:?}", data);
//result.headers().get("set-cookie").unwrap()
//println!( "{:#?}" , serde_json::from_str::<String>( &my_json.replace('"', "") ) );
}
fn takes_str(s: &str) { }
#[rocket::main]
async fn main() -> Result<()> {
rocket::build()
.mount("/", routes![callback])
.mount("/", routes![login])
.ignite()
.await
.unwrap()
.launch()
.await;
Ok(())
}
Best regards
Michael Ryom