Skip to content

Commit

Permalink
bex: Add request body struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed May 16, 2022
1 parent cfc2899 commit e76903c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
14 changes: 14 additions & 0 deletions bex/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bex/Cargo.toml
Expand Up @@ -8,5 +8,5 @@ edition = "2021"
[dependencies]
anyhow = "1.0.57"
reqwest = { version = "0.11", features = ["json"] }
serde = "1.0.137"
serde = { version = "1.0.137", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
39 changes: 28 additions & 11 deletions bex/src/main.rs
Expand Up @@ -25,12 +25,21 @@ async fn main() -> anyhow::Result<()> {
let state = "state1";

// Step 2: Obtain a request token
let url = "https://getpocket.com/v3/oauth/request";
let mut body = HashMap::new();
body.entry("consumer_key").or_insert(consumer_key.as_str());
body.entry("redirect_uri").or_insert(redirect_uri);
body.entry("state").or_insert(state);
let resp = post(url, &body).await?;
#[derive(Debug, Serialize)]
struct OAuthRequestRequestBody<'a> {
consumer_key: &'a str,
redirect_uri: &'a str,
state: Option<&'a str>,
}
let resp = post(
"https://getpocket.com/v3/oauth/request",
&OAuthRequestRequestBody {
consumer_key: consumer_key.as_str(),
redirect_uri,
state: Some(state),
},
)
.await?;
// TODO: check status code
// <https://getpocket.com/developer/docs/authentication>
println!("{:#?}", resp);
Expand All @@ -56,11 +65,19 @@ async fn main() -> anyhow::Result<()> {
io::stdin().read_line(&mut buffer)?;

// Step 5: Convert a request token into a Pocket access token
let url = "https://getpocket.com/v3/oauth/authorize";
let mut body = HashMap::new();
body.entry("consumer_key").or_insert(consumer_key.as_str());
body.entry("code").or_insert(request_token);
let resp = post(url, &body).await?;
#[derive(Debug, Serialize)]
struct OAuthAuthorizeRequestBody<'a> {
consumer_key: &'a str,
code: &'a str,
}
let resp = post(
"https://getpocket.com/v3/oauth/authorize",
&OAuthAuthorizeRequestBody {
consumer_key: consumer_key.as_str(),
code: request_token,
},
)
.await?;
println!("{:#?}", resp);

// "{\"access_token\":\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx\",\"username\":\"xxxxxxx\",\"state\":\"state1\"}"
Expand Down

0 comments on commit e76903c

Please sign in to comment.