-
I want to use the fcm crate and to make it work, the documentation mentions the following: Get an ApplicationSecret instance by some means How? I need some guidance here. I downloaded a .json file from firebase which I included below. This however does not contain a client_secret (and no redirect_uris). How would I create an ApplicationSecret?
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The generated CLI for each API can serve as an example, and I recommend adapting the code accordingly. The file you have seems to be all that's ultimately needed for authentication, even though there are various ways to accomplish this. |
Beta Was this translation helpful? Give feedback.
-
For anyone stumbling upon this later, here is how I did it. Let's say you want to support either loading up an authenticator from the default environment, or from an explicit service account JSON file. We don't bother with struct MyConfig {
/// If given, we will use this service account to authenticate instead of the
/// default authentication method. This must a JSON key for the service account,
/// encoded in base64.
service_account_json_base_64: Option<String>,
}
impl MyConfig {
fn service_account_json(&self) -> Result<Option<String>> {
Ok(self
.service_account_json_base_64
.as_ref()
.map(|s| base64_to_string(s))
.transpose()
.context("Failed to decode service account JSON")?)
}
} Now, to build an authenticator, you can do this: impl MyConfig {
pub async fn get_authenticator(&self) -> Result<Authenticator<HttpsConnector<HttpConnector>>> {
let authenticator = match self.service_account_json()? {
Some(service_account_json) => {
let service_account_key: ServiceAccountKey =
serde_json::from_str(&service_account_json)
.context("Failed to parse service account JSON")?;
ServiceAccountAuthenticator::builder(service_account_key)
.build()
.await
.context("Failed to create service account authenticator")?
},
None => {
match ApplicationDefaultCredentialsAuthenticator::builder(
ApplicationDefaultCredentialsFlowOpts::default(),
)
.await
{
ApplicationDefaultCredentialsTypes::InstanceMetadata(auth) => auth
.build()
.await
.context("Unable to create instance metadata authenticator")?,
ApplicationDefaultCredentialsTypes::ServiceAccount(auth) => auth
.build()
.await
.context("Unable to create service account authenticator")?,
}
},
};
Ok(authenticator)
}
} Finally, you can use it like this: pub struct ProcurementApiClient {
hub: CloudCommercePartnerProcurementService<HttpsConnector<HttpConnector>>,
}
impl ProcurementApiClient {
pub fn new(authenticator: Authenticator<HttpsConnector<HttpConnector>>) -> Self {
let hub = CloudCommercePartnerProcurementService::new(
hyper::Client::builder().build(
hyper_rustls::HttpsConnectorBuilder::new()
.with_native_roots()
.unwrap()
.https_or_http()
.enable_http1()
.build(),
),
authenticator,
);
Self { hub }
}
pub async fn approve_account(&self, partner_id: &str, account_id: &str) -> Result<()> {
let req = ApproveAccountRequest {
approval_name: Some("signup".to_string()),
..Default::default()
};
let name = format!("providers/{}/accounts/{}", partner_id, account_id);
self.hub
.providers()
.accounts_approve(req, &name)
.doit()
.await
.context("Failed to approve account")?;
Ok(())
}
} |
Beta Was this translation helpful? Give feedback.
For anyone stumbling upon this later, here is how I did it.
Let's say you want to support either loading up an authenticator from the default environment, or from an explicit service account JSON file. We don't bother with
ApplicationSecret
like is used in the example code. First, add some configuration to load up a service account JSON: