[][src]Crate yup_oauth2

This library can be used to acquire oauth2.0 authentication for services.

For your application to use this library, you will have to obtain an application id and secret by following this guide (for Google services) respectively the documentation of the API provider you want to connect to.

Device Flow Usage

As the DeviceFlow involves polling, the DeviceFlowHelper should be used as means to adhere to the protocol, and remain resilient to all kinds of errors that can occour on the way.

Service account "flow"

When using service account credentials, no user interaction is required. The access token can be obtained automatically using the private key of the client (which you can download from the API provider). See examples/service_account/ for an example on how to use service account credentials. See developers.google.com for a detailed description of the protocol. This crate implements OAuth for Service Accounts based on the Google APIs; it may or may not work with other providers.

Installed Flow Usage

The InstalledFlow involves showing a URL to the user (or opening it in a browser) and then either prompting the user to enter a displayed code, or make the authorizing website redirect to a web server spun up by this library and running on localhost.

In order to use the interactive method, use the InstalledInteractive FlowType; for the redirect method, use InstalledRedirect, with the port number to let the server listen on.

You can implement your own AuthenticatorDelegate in order to customize the flow; the InstalledFlow uses the present_user_url method.

The returned Token is stored permanently in the given token storage in order to authorize future API requests to the same scopes.

extern crate serde_derive;
use yup_oauth2::{Authenticator, DefaultAuthenticatorDelegate, PollInformation, ConsoleApplicationSecret, MemoryStorage, GetToken};
use serde_json as json;
use std::default::Default;
use hyper::{Client, net::HttpsConnector};
use hyper_native_tls::NativeTlsClient;

let secret = json::from_str::<ConsoleApplicationSecret>(SECRET).unwrap().installed.unwrap();
let res = Authenticator::new(&secret, DefaultAuthenticatorDelegate,
                        <MemoryStorage as Default>::default(), None)
match res {
    Ok(t) => {
    // now you can use t.access_token to authenticate API calls within your
    // given scopes. It will not be valid forever, but Authenticator will automatically
    // refresh the token for you.
    Err(err) => println!("Failed to acquire token: {}", err),



Represents either 'installed' or 'web' applications in a json secrets file. See ConsoleApplicationSecret for more information


A generalized authenticator which will keep tokens valid and store them.


A type to facilitate reading and writing the json secret file as returned by the google developer console


Uses all default implementations by AuthenticatorDelegate, and makes the trait's implementation usable in the first place.


Implements the Oauth2 Device Flow It operates in two steps:


Serializes tokens to a JSON file on disk.


A storage that remembers values for one session only.


A storage that remembers nothing.


Contains state of pending authentication requests


Implements the Outh2 Refresh Token Flow.


A scheme for use in hyper::header::Authorization


A token source (GetToken) yielding OAuth tokens for services that use ServiceAccount authorization. This token source caches token and automatically renews expired ones.


JSON schema of secret service account key. You can obtain the key from the Cloud Console at https://console.cloud.google.com/.


Represents a token as returned by OAuth2 servers.



All known authentication types, for suitable constants


cf. https://developers.google.com/identity/protocols/OAuth2InstalledApp#choosingredirecturi


Encapsulates all possible results of a poll_token(...) operation


All possible outcomes of the refresh flow


A utility type to indicate how operations DeviceFlowHelper operations should be retried


Represents all implemented token types





A partially implemented trait to interact with the Authenticator


A provider for authorization tokens, yielding tokens valid for a given scope. The api_key() method is an alternative in case there are no scopes or if no user is involved.


Implements a specialized storage to set and retrieve Token instances. The scope_hash represents the signature of the scopes for which the given token should be stored or retrieved. For completeness, the underlying, sorted scopes are provided as well. They might be useful for presentation to the user.



Read an application secret from a JSON string.


Read an application secret from a file.


Read a service account key from a JSON file. You can download the JSON keys from the Google Cloud Console or the respective console of your service provider.