| 261 | } |
| 262 | |
| 263 | async function getAuthToken(email, scopes = chrome.runtime.getManifest().oauth2.scopes, prompt) { |
| 264 | const redirectURL = chrome.identity.getRedirectURL(); |
| 265 | const state = getUUID(); |
| 266 | const auth_params = { |
| 267 | client_id: CLIENT_ID, |
| 268 | include_granted_scopes: true, |
| 269 | login_hint: email, |
| 270 | redirect_uri: redirectURL, |
| 271 | response_type: 'token', |
| 272 | scope: scopes.join(' '), |
| 273 | state, |
| 274 | ...(prompt && {prompt}) |
| 275 | }; |
| 276 | const url = `${GOOGLE_API_HOST}/o/oauth2/v2/auth?${new URLSearchParams(Object.entries(auth_params))}`; |
| 277 | const responseURL = await chrome.identity.launchWebAuthFlow({url, interactive: true}); |
| 278 | const search = new URLSearchParams(new URL(responseURL).hash.replace(/^#/, '')); |
| 279 | if (search.get('state') !== state) { |
| 280 | throw new Error('oauth2/v2/auth: wrong state parameter'); |
| 281 | } |
| 282 | return search.get('access_token'); |
| 283 | } |
| 284 | |
| 285 | async function getAuthTokens(authCode) { |
| 286 | const url = 'https://oauth2.googleapis.com/token'; |