pub struct Client<P> {
pub provider: P,
pub client_id: String,
pub client_secret: String,
pub redirect_uri: Option<String>,
}
Expand description
OAuth 2.0 client.
Fields§
§provider: P
OAuth provider.
client_id: String
Client ID.
client_secret: String
Client secret.
redirect_uri: Option<String>
Redirect URI.
Implementations§
Source§impl<P: Provider> Client<P>
impl<P: Provider> Client<P>
Sourcepub fn new(
provider: P,
client_id: String,
client_secret: String,
redirect_uri: Option<String>,
) -> Self
pub fn new( provider: P, client_id: String, client_secret: String, redirect_uri: Option<String>, ) -> Self
Creates a client.
§Examples
use inth_oauth2::Client;
use inth_oauth2::provider::google::Installed;
let client = Client::new(
Installed,
String::from("CLIENT_ID"),
String::from("CLIENT_SECRET"),
Some(String::from("urn:ietf:wg:oauth:2.0:oob")),
);
Examples found in repository?
examples/github.rs (lines 12-17)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 GitHub,
14 String::from("01774654cd9a6051e478"),
15 String::from("9f14d16d95d605e715ec1a9aecec220d2565fd5c"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(Some("user"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27}
More examples
examples/google-web.rs (lines 12-17)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Web,
14 String::from("143225766783-0h4h5ktpvhc7kqp6ohbpd2sssqrap57n.apps.googleusercontent.com"),
15 String::from("7Xjn-vRN-8qsz3Zh9zZGkHsM"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27}
examples/imgur.rs (lines 12-17)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Imgur,
14 String::from("505c8ca804230e0"),
15 String::from("c898d8cf28404102752b2119a3a1c6aab49899c8"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(None, None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27
28 let token = client.refresh_token(&http_client, token, None).unwrap();
29 println!("{:?}", token);
30}
examples/google-installed.rs (lines 12-17)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Installed,
14 String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
15 String::from("3kZ5WomzHFlN2f_XbhkyPd3o"),
16 Some(String::from(REDIRECT_URI_OOB)),
17 );
18
19 let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27
28 let token = client.refresh_token(&http_client, token, None).unwrap();
29 println!("{:?}", token);
30}
Sourcepub fn auth_uri(&self, scope: Option<&str>, state: Option<&str>) -> Url
pub fn auth_uri(&self, scope: Option<&str>, state: Option<&str>) -> Url
Returns an authorization endpoint URI to direct the user to.
§Examples
use inth_oauth2::Client;
use inth_oauth2::provider::google::Installed;
let client = Client::new(
Installed,
String::from("CLIENT_ID"),
String::from("CLIENT_SECRET"),
Some(String::from("urn:ietf:wg:oauth:2.0:oob")),
);
let auth_uri = client.auth_uri(
Some("https://www.googleapis.com/auth/userinfo.email"),
None,
);
Examples found in repository?
examples/github.rs (line 19)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 GitHub,
14 String::from("01774654cd9a6051e478"),
15 String::from("9f14d16d95d605e715ec1a9aecec220d2565fd5c"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(Some("user"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27}
More examples
examples/google-web.rs (line 19)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Web,
14 String::from("143225766783-0h4h5ktpvhc7kqp6ohbpd2sssqrap57n.apps.googleusercontent.com"),
15 String::from("7Xjn-vRN-8qsz3Zh9zZGkHsM"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27}
examples/imgur.rs (line 19)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Imgur,
14 String::from("505c8ca804230e0"),
15 String::from("c898d8cf28404102752b2119a3a1c6aab49899c8"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(None, None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27
28 let token = client.refresh_token(&http_client, token, None).unwrap();
29 println!("{:?}", token);
30}
examples/google-installed.rs (line 19)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Installed,
14 String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
15 String::from("3kZ5WomzHFlN2f_XbhkyPd3o"),
16 Some(String::from(REDIRECT_URI_OOB)),
17 );
18
19 let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27
28 let token = client.refresh_token(&http_client, token, None).unwrap();
29 println!("{:?}", token);
30}
Sourcepub fn request_token(
&self,
http_client: &Client,
code: &str,
) -> Result<P::Token, ClientError>
pub fn request_token( &self, http_client: &Client, code: &str, ) -> Result<P::Token, ClientError>
Requests an access token using an authorization code.
Examples found in repository?
examples/github.rs (line 25)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 GitHub,
14 String::from("01774654cd9a6051e478"),
15 String::from("9f14d16d95d605e715ec1a9aecec220d2565fd5c"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(Some("user"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27}
More examples
examples/google-web.rs (line 25)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Web,
14 String::from("143225766783-0h4h5ktpvhc7kqp6ohbpd2sssqrap57n.apps.googleusercontent.com"),
15 String::from("7Xjn-vRN-8qsz3Zh9zZGkHsM"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27}
examples/imgur.rs (line 25)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Imgur,
14 String::from("505c8ca804230e0"),
15 String::from("c898d8cf28404102752b2119a3a1c6aab49899c8"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(None, None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27
28 let token = client.refresh_token(&http_client, token, None).unwrap();
29 println!("{:?}", token);
30}
examples/google-installed.rs (line 25)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Installed,
14 String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
15 String::from("3kZ5WomzHFlN2f_XbhkyPd3o"),
16 Some(String::from(REDIRECT_URI_OOB)),
17 );
18
19 let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27
28 let token = client.refresh_token(&http_client, token, None).unwrap();
29 println!("{:?}", token);
30}
Source§impl<P> Client<P>
impl<P> Client<P>
Sourcepub fn refresh_token(
&self,
http_client: &Client,
token: P::Token,
scope: Option<&str>,
) -> Result<P::Token, ClientError>
pub fn refresh_token( &self, http_client: &Client, token: P::Token, scope: Option<&str>, ) -> Result<P::Token, ClientError>
Refreshes an access token.
See RFC 6749, section 6.
Examples found in repository?
examples/imgur.rs (line 28)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Imgur,
14 String::from("505c8ca804230e0"),
15 String::from("c898d8cf28404102752b2119a3a1c6aab49899c8"),
16 Some(String::from("https://cmcenroe.me/oauth2-paste/")),
17 );
18
19 let auth_uri = client.auth_uri(None, None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27
28 let token = client.refresh_token(&http_client, token, None).unwrap();
29 println!("{:?}", token);
30}
More examples
examples/google-installed.rs (line 28)
9fn main() {
10 let http_client = reqwest::Client::new();
11
12 let client = Client::new(
13 Installed,
14 String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
15 String::from("3kZ5WomzHFlN2f_XbhkyPd3o"),
16 Some(String::from(REDIRECT_URI_OOB)),
17 );
18
19 let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None);
20 println!("{}", auth_uri);
21
22 let mut code = String::new();
23 io::stdin().read_line(&mut code).unwrap();
24
25 let token = client.request_token(&http_client, code.trim()).unwrap();
26 println!("{:?}", token);
27
28 let token = client.refresh_token(&http_client, token, None).unwrap();
29 println!("{:?}", token);
30}
Sourcepub fn ensure_token(
&self,
http_client: &Client,
token: P::Token,
) -> Result<P::Token, ClientError>
pub fn ensure_token( &self, http_client: &Client, token: P::Token, ) -> Result<P::Token, ClientError>
Ensures an access token is valid by refreshing it if necessary.
Trait Implementations§
impl<P: Eq> Eq for Client<P>
impl<P> StructuralPartialEq for Client<P>
Auto Trait Implementations§
impl<P> Freeze for Client<P>where
P: Freeze,
impl<P> RefUnwindSafe for Client<P>where
P: RefUnwindSafe,
impl<P> Send for Client<P>where
P: Send,
impl<P> Sync for Client<P>where
P: Sync,
impl<P> Unpin for Client<P>where
P: Unpin,
impl<P> UnwindSafe for Client<P>where
P: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more