From 08f919696438c7dd51db98fa38a79baaac907de3 Mon Sep 17 00:00:00 2001 From: David Tulig Date: Tue, 3 Jan 2017 20:55:21 -0600 Subject: [PATCH] Create a token request to exhange authorization code with token. Currently, the InstalledApplication module retrieves the authorization code from the user but when it tries to exchange that code for an oauth token it queries the wrong endpoint (accounts.google.com). It needs to query the endpoint to exchange tokens, not the original accounts endpoint. This is documented at https://developers.google.com/identity/protocols/OAuth2InstalledApp#handlingtheresponse. --- .../src/Network/Google/Auth/InstalledApplication.hs | 2 +- gogol/src/Network/Google/Internal/Auth.hs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gogol/src/Network/Google/Auth/InstalledApplication.hs b/gogol/src/Network/Google/Auth/InstalledApplication.hs index 2b1d0e3255..d5e7faad37 100644 --- a/gogol/src/Network/Google/Auth/InstalledApplication.hs +++ b/gogol/src/Network/Google/Auth/InstalledApplication.hs @@ -109,7 +109,7 @@ exchangeCode :: (MonadIO m, MonadCatch m) -> Manager -> m (OAuthToken s) exchangeCode c n = refreshRequest $ - accountsRequest + tokenRequest { Client.requestBody = textBody $ "grant_type=authorization_code" <> "&client_id=" <> toQueryParam (_clientId c) diff --git a/gogol/src/Network/Google/Internal/Auth.hs b/gogol/src/Network/Google/Internal/Auth.hs index 938a58e252..4bf7f47eec 100644 --- a/gogol/src/Network/Google/Internal/Auth.hs +++ b/gogol/src/Network/Google/Internal/Auth.hs @@ -260,6 +260,18 @@ accountsRequest = Client.defaultRequest ] } +tokenRequest :: Client.Request +tokenRequest = Client.defaultRequest + { Client.host = "www.googleapis.com" + , Client.port = 443 + , Client.secure = True + , Client.method = "POST" + , Client.path = "/oauth2/v4/token" + , Client.requestHeaders = + [ (hContentType, "application/x-www-form-urlencoded") + ] + } + refreshRequest :: (MonadIO m, MonadCatch m) => Client.Request -> Logger