Last active
February 8, 2018 01:26
-
-
Save kevin-cantwell/add78eb766d139dc64429dc3ea9f51b0 to your computer and use it in GitHub Desktop.
Exchanges a Google Oauth2 refresh token for a bearer token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"io" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
) | |
func main() { | |
data := url.Values{} | |
data.Set("refresh_token", os.Args[1]) | |
data.Set("grant_type", "refresh_token") | |
data.Set("client_id", os.Getenv("GOOGLE_CLIENT_ID")) | |
data.Set("client_secret", os.Getenv("GOOGLE_CLIENT_SECRET")) | |
req, err := http.NewRequest("POST", "https://www.googleapis.com/oauth2/v4/token", bytes.NewBufferString(data.Encode())) | |
if err != nil { | |
panic(err) | |
} | |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
io.Copy(os.Stdout, resp.Body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment