OAuth2 login and token refresh
Mechanism
GoogleSheets registers StartLogin, FinishLogin and Refresh in its OAuth record (lines 64–76). StartLogin builds a Google authorization URL with the client identifier, a read-only spreadsheets scope, access_type = "offline", prompt = "consent", state, and the callback URL. It returns Context = null, so this flow keeps no verifier between the two steps.1
FinishLogin checks the callback query for an error field first and raises Error.Record("DataSource.Error", ...) when it is present. Otherwise it posts the code to the token endpoint with grant_type = "authorization_code" and parses the JSON response.2
Refresh is a separate POST with grant_type = "refresh_token", the stored refresh token, the client identifier and the client secret. It returns the parsed response without further checks.3 The secret in this archive is <redacted> (line 5), so the values here are a reading aid, not a working registration.1
Choosing this flow
GoogleSheets illustrates authorization-code and refresh-token exchanges. Microsoft's OAuth guidance advises against confidential secrets in client-side OAuth flows because users can inspect the extension or its traffic. Asana supplies a PKCE comparison and carries its verifier in Context. It registers no Refresh callback; GetTasks explicitly calls RefreshToken() in its request path.5
Anaplan registers Refresh = (clientApplication, dataSourcePath, oldCredential), while GoogleSheets uses two arguments. Microsoft documents both original and advanced callback signatures. The advanced form receives the previous credential record; the original form receives the refresh token. Keep the callback body aligned with the selected signature.43
Adaptation and failure paths
The error branch checks only the error field. A callback that carries a code but a different failure field continues to the token request, and a successful browser redirect is not evidence that the exchange succeeded.2 The state value passed to StartLogin is never compared with the callback in this file. The archive does not show whether the host validates it, so review callback-state handling separately before adopting the flow.2
Neither the token request nor Refresh sets ManualStatusHandling, so a non-2xx token response follows the platform's default status handling. Check the message your users see for an expired refresh token before relying on it.3
Refresh returns the parsed body unchanged, including any fields the provider supplies. It has no local branch to preserve an old refresh token when the response omits one. Test token rotation and omission with the host; returning the full record is not evidence that those fields are discarded.3
Keep CallbackUrl identical in the authorization request and the token request. This file uses the same constant for the authorization redirect_uri (line 18), the CallbackUri (line 22), and the token exchange (line 40).2
See credential access for how saved credentials reach a data source.
Examples
GoogleSheets/GoogleSheets.pq · lines 48–62 Open original source ↗
Refresh = (resourceUrl, refresh_token) =>
let
Response = Web.Contents("https://oauth2.googleapis.com/token", [
Content = Text.ToBinary(Uri.BuildQueryString([
grant_type = "refresh_token",
client_id = ClientId,
client_secret = ClientSecret,
refresh_token = refresh_token
])),
Headers = [
#"Content-type" = "application/x-www-form-urlencoded",
#"Accept" = "application/json"]]),
Parts = Json.Document(Response)
in
Parts;
The login start builds the authorization URL and returns no verifier:
GoogleSheets/GoogleSheets.pq · lines 9–26 Open original source ↗
StartLogin = (resourceUrl, state, display) =>
let
AuthorizeUrl = "https://accounts.google.com/o/oauth2/v2/auth?" & Uri.BuildQueryString([
client_id = ClientId,
scope = "https://www.googleapis.com/auth/spreadsheets.readonly",
response_type = "code",
access_type = "offline",
prompt = "consent",
state = state,
redirect_uri = CallbackUrl])
in
[
LoginUri = AuthorizeUrl,
CallbackUri = CallbackUrl,
WindowHeight = 780,
WindowWidth = 480,
Context = null
];
The callback checks for a provider error, then exchanges the code:
GoogleSheets/GoogleSheets.pq · lines 28–46 Open original source ↗
FinishLogin = (context, callbackUri, state) =>
let
Parts = Uri.Parts(callbackUri)[Query],
Response = if (Parts[error]? <> null) then
error Error.Record("DataSource.Error", Parts[error])
else
Web.Contents("https://oauth2.googleapis.com/token", [
Content = Text.ToBinary(Uri.BuildQueryString([
client_id = ClientId,
client_secret = ClientSecret,
grant_type = "authorization_code",
code = Parts[code],
redirect_uri = CallbackUrl])),
Headers = [
#"Content-type" = "application/x-www-form-urlencoded",
#"Accept" = "application/json"]]),
token = Json.Document(Response)
in
token;
-
GoogleSheets/GoogleSheets.pq lines 9–26 build the authorization URL and return
Context = null; line 5 holds the redacted secret, and lines 64–76 register OAuth. ↩↩ -
GoogleSheets/GoogleSheets.pq lines 28–46: the
errorcheck at line 31, the authorization-code exchange at lines 34–43, and the sharedCallbackUrlat lines 18, 22 and 40.FinishLogintakes astateargument (line 28) but its body never reads it. ↩↩↩↩ -
GoogleSheets/GoogleSheets.pq lines 48–62 define
Refresh. ItsWeb.Contentsoptions contain onlyContentandHeaders, with noManualStatusHandling. ↩↩↩↩ -
Anaplan/Anaplan.pq lines 92–97 define a three-argument
Refreshthat readsoldCredential[tokenHost]. ↩ -
Asana/Asana.m lines 83–89 register OAuth without a
Refreshfield; lines 173–198 callRefreshToken()fromGetTasksand define it. ↩