Google OAuth2 authentication library for MoonBit
Dependencies
{
"deps": {
"ryota0624/googleauth": "0.1.0",
"ryota0624/oauth2": "0.1.2",
"mizchi/x": "0.1.3"
}
}///|
pub fn main() {
// Create OAuth2 client with PKCE support
let client = GoogleOAuth2Client::{
client_id: "your-client-id",
client_secret: "your-client-secret",
redirect_uri: "http://localhost:3000/callback",
auth_uri: GOOGLE_AUTH_ENDPOINT,
token_uri: GOOGLE_TOKEN_ENDPOINT,
scopes: ["openid", "email", "profile"],
pkce_verifier: None,
}
// Build authorization URL
let auth_url = client.build_auth_url()
println("Visit: " + auth_url)
// After user grants permission, verify callback
match client.verify_callback("code=...&state=...") {
Ok(_) => println("Authorization successful")
Err(err) => println("Authorization failed")
}
}///|
async fn main() {
// Automatically detect authentication method (ADC → Metadata Server)
match auto_detect_and_authenticate() {
Ok(auth_client) => {
let api_client = new_api_client(auth_client)
match api_client.refresh_token_if_needed(0) {
Ok(client) =>
match client.get_valid_token(0) {
Ok(token) => println("Token: " + token)
Err(_) => println("Failed to get token")
}
Err(_) => println("Failed to authenticate")
}
}
Err(_) => println("No authentication method available")
}
}basic_scopes() // openid, email, profile
drive_scopes() // Google Drive
gmail_scopes() // Gmail
calendar_scopes() // Google Calendar
docs_scopes() // Google Docs
sheets_scopes() // Google Sheetsmoon test # 80+ tests
moon fmt lib # Format codeInstall
Download zipGoogle OAuth2 authentication library for MoonBit
Dependencies