@google/clasp
Version:
Develop Apps Script Projects locally
37 lines (36 loc) • 1.16 kB
JavaScript
export class AuthorizationCodeFlow {
constructor(oauth2client) {
this.oauth2Client = oauth2client;
}
async authorize(scopes) {
const scope = Array.isArray(scopes) ? scopes.join(' ') : scopes;
const redirectUri = await this.getRedirectUri();
const authUrl = this.oauth2Client.generateAuthUrl({
redirect_uri: redirectUri,
access_type: 'offline',
scope: scope,
});
const code = await this.promptAndReturnCode(authUrl);
const tokens = await this.oauth2Client.getToken({
code,
redirect_uri: redirectUri,
});
this.oauth2Client.setCredentials(tokens.tokens);
return this.oauth2Client;
}
async getRedirectUri() {
throw new Error('Not implemented');
}
async promptAndReturnCode(_authorizationUrl) {
throw new Error('Not implemented');
}
}
export function parseAuthResponseUrl(url) {
const urlParts = new URL(url, 'http://localhost/').searchParams;
const code = urlParts.get('code');
const error = urlParts.get('error');
return {
code,
error,
};
}