Skip to content

Instantly share code, notes, and snippets.

@aidiss
Created February 3, 2024 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aidiss/1483463d2b9823391527520ee110d717 to your computer and use it in GitHub Desktop.
Save aidiss/1483463d2b9823391527520ee110d717 to your computer and use it in GitHub Desktop.
from google_auth_oauthlib.flow import InstalledAppFlow
import socket
import re
from urllib.parse import unquote
CLIENT_SECRET_JSON_PATH = "./client_secret.json"
SERVER, PORT = "127.0.0.1", 8080
flow = InstalledAppFlow.from_client_secrets_file(
CLIENT_SECRET_JSON_PATH,
scopes=["https://www.googleapis.com/auth/adwords"],
redirect_uri=f"http://{SERVER}:{PORT}",
)
print(flow.authorization_url()[0])
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((SERVER, PORT))
sock.listen(1)
connection, address = sock.accept()
data = connection.recv(1024)
decoded = unquote(data)
match = re.search(r"GET\s\/\?(.*) ", decoded)
params = match.group(1)
pairs = [pair.split("=") for pair in params.split("&")]
params = {key: val for key, val in pairs}
print(params)
token = flow.fetch_token(code=params["code"])
refresh_token = token.get(
"refresh_token",
"No refresh token, it is only available on the first authorization",
)
print(refresh_token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment