pub struct Authenticator { /* private fields */ }Expand description
Handles the iNaturalist OAuth2 flow to obtain an API token.
This struct is used to configure the authenticator and initiate the OAuth2 flow.
Implementations§
Source§impl Authenticator
impl Authenticator
Sourcepub fn new(client_id: String, client_secret: String) -> Self
pub fn new(client_id: String, client_secret: String) -> Self
Creates a new Authenticator.
§Arguments
client_id- The iNaturalist application’s client ID.client_secret- The iNaturalist application’s client secret.
Sourcepub fn with_redirect_server_port(self, port: u16) -> Self
pub fn with_redirect_server_port(self, port: u16) -> Self
Sets the port for the local redirect server.
The default port is 8080.
Generates the authorization URL and PKCE verifier.
This is the first step in the OAuth2 flow. The user should be redirected
to the returned URL. The pkce_verifier must be stored and used when
calling exchange_code.
Sourcepub async fn exchange_code(
&self,
code: AuthorizationCode,
pkce_verifier: PkceVerifier,
) -> Result<TokenDetails, Box<dyn Error>>
pub async fn exchange_code( &self, code: AuthorizationCode, pkce_verifier: PkceVerifier, ) -> Result<TokenDetails, Box<dyn Error>>
Exchanges an authorization code for an iNaturalist API token.
This is the final step in the OAuth2 flow. The code is obtained from
the iNaturalist redirect, and pkce_verifier is the one generated by
authorization_url.
Sourcepub fn listen_for_redirect(&self) -> Result<AuthorizationCode, Box<dyn Error>>
pub fn listen_for_redirect(&self) -> Result<AuthorizationCode, Box<dyn Error>>
Listens for the redirect from iNaturalist and extracts the authorization code.
This method starts a temporary local server to catch the redirect from iNaturalist after the user has authorized the application. It’s a blocking call that waits for the redirect and returns the authorization code.
Sourcepub async fn get_api_token(self) -> Result<TokenDetails, Box<dyn Error>>
pub async fn get_api_token(self) -> Result<TokenDetails, Box<dyn Error>>
Initiates the OAuth2 flow to get an iNaturalist API token.
This method opens the user’s web browser to the iNaturalist authorization page. After the user authorizes the application, it completes the OAuth2 flow, obtains an access token, and then exchanges it for a long-lived API token.