Created
February 3, 2024 17:11
-
-
Save aidiss/1483463d2b9823391527520ee110d717 to your computer and use it in GitHub Desktop.
Fixed version of https://developers.google.com/google-ads/api/samples/generate-user-credentials#python
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
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