#How to access the oauth2 response to make API requests?
1 messages · Page 1 of 1 (latest)
Can you describe maybe an example API you are trying to access and what part of it doesn't work? E.g. am I assuming correctly that the login per-se works?
I think you are trying to figure out how to attach the oauth token to an outgoing http request?
Since you're using yew-oauth2 and that's supposedly intimately relevant to the problem, have you tried their example https://github.com/ctron/yew-oauth2/tree/091ad23cd93cd5502db5e0a90a8e28924e8f0743/yew-oauth2-example or contacting their author?
It seems hard to reproduce, since I don't have a yahoo client_id, anyway
I think the Oauth token is provided via a contextual OAuth2Context https://github.com/ctron/yew-oauth2/blob/091ad23cd93cd5502db5e0a90a8e28924e8f0743/src/context.rs#L29, if that's the question
This can be used via use_context in function components, e.g. https://github.com/ctron/yew-oauth2/blob/091ad23cd93cd5502db5e0a90a8e28924e8f0743/src/components/authenticated.rs#LL17C21-L17C21
and via ctx.link().context() in struct components, e.g. https://github.com/ctron/yew-oauth2/blob/091ad23cd93cd5502db5e0a90a8e28924e8f0743/yew-oauth2-example/src/components/component.rs#L20-L22
Hope that helps
Thanks a alot! Your assumptions are correct and the links should help. I'll try them and will let it known if they helped or not.
I've managed to make a valid api request using this link(https://github.com/ctron/yew-oauth2/tree/091ad23cd93cd5502db5e0a90a8e28924e8f0743/yew-oauth2-example)! I've basically integrated functional.rs, view.rs, and app.rs to my own code. The request code is somewhat similar to this:
use crate::model::video::Video;
use super::view::ViewAuthContext;
use gloo_net::http::Request;
use yew::prelude::*;
use yew_oauth2::prelude::*;
#[function_component(ViewAuthInfoFunctional)]
pub fn view_info() -> Html {
let auth = use_context::<OAuth2Context>();
let auth2 = use_context::<OAuth2Context>();
let videos = use_state(|| vec![]);
{
use_effect_with_deps(
move |_| {
wasm_bindgen_futures::spawn_local(async move {
let ctx = auth2.expect("TODO: add error handling");
let auth = ctx.authentication().expect("TODO: add error handling");
let request = Request::get("https://cors-anywhere.herokuapp.com/https://fantasysports.yahooapis.com/fantasy/v2/league/418.l.9097/standings?format=json")
.header("Authorization", format!("Bearer {}", &auth.access_token).as_str())
// .header("Access-Control-Allow-Origin", "https://43c5-46-1-240-92.eu.ngrok.io")
// .header("Access-Control-Allow-Methods", "GET")
// .header("Access-Control-Allow-Headers", "Content-Type, Authorization")
;
let response = request.send().await.unwrap();
log::debug!("response:\n{:?}", response);
});
|| ()
},
(),
);
}
html!(
if let Some(auth) = auth {
<h2> { "Function component example"} </h2>
<ViewAuthContext {auth} />
} else {
{ "OAuth2 context not found." }
}
)
}
Thanks a lot!
My issue is resolved, I am not sure how to mark it as such even if I'm able to.