apps-script-oauth2
An OAuth2 library for Google Apps Script.
AI Summary
OAuth2 for Apps Script
Library for creating, authorizing, and refreshing OAuth2 tokens in GAS.
Target Users
Intermediate GAS developers building add-ons or scripts needing access to Google APIs or external OAuth2 providers.
Problems Solved
Handling OAuth2 authentication and token management for Google APIs or external providers beyond GAS built-in and advanced services.
Script ID
- In GAS Editor: Click "Libraries +" → Paste into "Script ID" field → Click "Look up"
- "apps-script-oauth2" will appear in the search results
- Select the latest version (highest number) from "Version" dropdown
- Click "Add"
Tags
Main Features
1
Easy Setup
Use OAuth2.createService to set endpoints, client ID/secret, callback; persist tokens with property store.
2
Auto Refresh
Automatically refresh expired tokens. Cache and lock to save PropertiesService quota and prevent concurrent refreshes.
3
Multi-Provider
Samples for Google Drive, FitBit, Twitter. Supports service accounts and alternative grant types.
Examples
Main Functions
| Function | Description |
|---|---|
| OAuth2.createService | Create named OAuth2 service |
| getAuthorizationUrl | Get authorization URL for user approval |
| handleCallback | Handle OAuth callback in function |
Examples
Create Drive OAuth2 Service
/**
* Drive OAuth2サービス作成
* ・サービス名指定: OAuth2.createService('drive')
* ・Google共通エンドポイント設定: .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth').setTokenUrl('https://accounts.google.com/o/oauth2/token')
* ・クライアント・コールバック・ストア設定: .setClientId('...').setClientSecret('...').setCallbackFunction('authCallback').setPropertyStore(PropertiesService.getUserProperties())
*/
function getDriveService_() {
return OAuth2.createService('drive')
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
.setClientId('...')
.setClientSecret('...')
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
.setScope('https://www.googleapis.com/auth/drive')
.setParam('login_hint', Session.getEffectiveUser().getEmail())
.setParam('access_type', 'offline')
.setParam('prompt', 'consent');
}Show Authorization URL
/**
* サイドバーで認可URLを表示
* ・サービス取得: getDriveService_()
* ・アクセス確認: if (!driveService.hasAccess())
* ・URL生成・表示: driveService.getAuthorizationUrl() をHtmlServiceでリンク表示
*/
function showSidebar() {
var driveService = getDriveService_();
if (!driveService.hasAccess()) {
var authorizationUrl = driveService.getAuthorizationUrl();
var template = HtmlService.createTemplate(
'<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
'Reopen the sidebar when the authorization is complete.');
template.authorizationUrl = authorizationUrl;
var page = template.evaluate();
DocumentApp.getUi().showSidebar(page);
}
}Handle Callback
/**
* OAuthコールバック処理
* ・サービス取得: getDriveService_()
* ・コールバック処理: driveService.handleCallback(request)
* ・結果表示: isAuthorized で成功/否認メッセージをHtmlServiceで返す
*/
function authCallback(request) {
var driveService = getDriveService_();
var isAuthorized = driveService.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Success! You can close this tab.');
} else {
return HtmlService.createHtmlOutput('Denied. You can close this tab');
}
}