gen_user_cred.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/env python
  2. # Copyright 2018 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # https://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """This example will create an OAuth2 refresh token for the Google Ads API.
  16. This example works with both web and desktop app OAuth client ID types.
  17. https://console.cloud.google.com
  18. IMPORTANT: For web app clients types, you must add "http://127.0.0.1" to the
  19. "Authorized redirect URIs" list in your Google Cloud Console project before
  20. running this example. Desktop app client types do not require the local
  21. redirect to be explicitly configured in the console.
  22. Once complete, download the credentials and save the file path so it can be
  23. passed into this example.
  24. This example is a very simple implementation, for a more detailed example see:
  25. https://developers.google.com/identity/protocols/oauth2/web-server#python
  26. """
  27. import argparse
  28. import hashlib
  29. import os
  30. import re
  31. import socket
  32. import sys
  33. from urllib.parse import unquote
  34. # If using Web flow, the redirect URL must match exactly what’s configured in GCP for
  35. # the OAuth client. If using Desktop flow, the redirect must be a localhost URL and
  36. # is not explicitly set in GCP.
  37. from google_auth_oauthlib.flow import Flow
  38. _SCOPE = "https://www.googleapis.com/auth/adwords"
  39. _SERVER = "127.0.0.1"
  40. _PORT = 8080
  41. _REDIRECT_URI = f"http://{_SERVER}:{_PORT}"
  42. def main(client_secrets_path, scopes):
  43. """The main method, starts a basic server and initializes an auth request.
  44. Args:
  45. client_secrets_path: a path to where the client secrets JSON file is
  46. located on the machine running this example.
  47. scopes: a list of API scopes to include in the auth request, see:
  48. https://developers.google.com/identity/protocols/oauth2/scopes
  49. """
  50. flow = Flow.from_client_secrets_file(client_secrets_path, scopes=scopes)
  51. flow.redirect_uri = _REDIRECT_URI
  52. # Create an anti-forgery state token as described here:
  53. # https://developers.google.com/identity/protocols/OpenIDConnect#createxsrftoken
  54. passthrough_val = hashlib.sha256(os.urandom(1024)).hexdigest()
  55. authorization_url, state = flow.authorization_url(
  56. access_type="offline",
  57. state=passthrough_val,
  58. prompt="consent",
  59. include_granted_scopes="true",
  60. )
  61. # Prints the authorization URL so you can paste into your browser. In a
  62. # typical web application you would redirect the user to this URL, and they
  63. # would be redirected back to "redirect_url" provided earlier after
  64. # granting permission.
  65. print("Paste this URL into your browser: ")
  66. print(authorization_url)
  67. print(f"\nWaiting for authorization and callback to: {_REDIRECT_URI}")
  68. # Retrieves an authorization code by opening a socket to receive the
  69. # redirect request and parsing the query parameters set in the URL.
  70. code = unquote(_get_authorization_code(passthrough_val))
  71. # Pass the code back into the OAuth module to get a refresh token.
  72. flow.fetch_token(code=code)
  73. refresh_token = flow.credentials.refresh_token
  74. print(f"\nYour refresh token is: {refresh_token}\n")
  75. print(
  76. "Add your refresh token to your client library configuration as "
  77. "described here: "
  78. "https://developers.google.com/google-ads/api/docs/client-libs/python/configuration"
  79. )
  80. def _get_authorization_code(passthrough_val):
  81. """Opens a socket to handle a single HTTP request containing auth tokens.
  82. Args:
  83. passthrough_val: an anti-forgery token used to verify the request
  84. received by the socket.
  85. Returns:
  86. a str access token from the Google Auth service.
  87. """
  88. # Open a socket at _SERVER:_PORT and listen for a request
  89. sock = socket.socket()
  90. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  91. sock.bind((_SERVER, _PORT))
  92. sock.listen(1)
  93. connection, address = sock.accept()
  94. data = connection.recv(1024)
  95. # Parse the raw request to retrieve the URL query parameters.
  96. params = _parse_raw_query_params(data)
  97. try:
  98. if not params.get("code"):
  99. # If no code is present in the query params then there will be an
  100. # error message with more details.
  101. error = params.get("error")
  102. message = f"Failed to retrieve authorization code. Error: {error}"
  103. raise ValueError(message)
  104. elif params.get("state") != passthrough_val:
  105. message = "State token does not match the expected state."
  106. raise ValueError(message)
  107. else:
  108. message = "Authorization code was successfully retrieved."
  109. except ValueError as error:
  110. print(error)
  111. sys.exit(1)
  112. finally:
  113. response = ("HTTP/1.1 200 OK\n"
  114. "Content-Type: text/html\n\n"
  115. f"<b>{message}</b>"
  116. "<p>Please check the console output.</p>\n")
  117. connection.sendall(response.encode())
  118. connection.close()
  119. return params.get("code")
  120. def _parse_raw_query_params(data):
  121. """Parses a raw HTTP request to extract its query params as a dict.
  122. Note that this logic is likely irrelevant if you're building OAuth logic
  123. into a complete web application, where response parsing is handled by a
  124. framework.
  125. Args:
  126. data: raw request data as bytes.
  127. Returns:
  128. a dict of query parameter key value pairs.
  129. """
  130. # Decode the request into a utf-8 encoded string
  131. decoded = data.decode("utf-8")
  132. # Use a regular expression to extract the URL query parameters string
  133. match = re.search("GET\s\/\?(.*) ", decoded)
  134. params = match.group(1)
  135. # Split the parameters to isolate the key/value pairs
  136. pairs = [pair.split("=") for pair in params.split("&")]
  137. # Convert pairs to a dict to make it easy to access the values
  138. return {key: val for key, val in pairs}
  139. if __name__ == "__main__":
  140. parser = argparse.ArgumentParser(
  141. description=(
  142. "Generates OAuth2 refresh token using the Web application flow. "
  143. "To retrieve the necessary client_secrets JSON file, first "
  144. "generate OAuth 2.0 credentials of type Web application in the "
  145. "Google Cloud Console (https://console.cloud.google.com). "
  146. "Make sure 'http://_SERVER:_PORT' is included the list of "
  147. "'Authorized redirect URIs' for this client ID."),)
  148. # The following argument(s) should be provided to run the example.
  149. parser.add_argument(
  150. "-c",
  151. "--client_secrets_path",
  152. required=True,
  153. type=str,
  154. help=("Path to the client secrets JSON file from the Google Developers "
  155. "Console that contains your client ID, client secret, and "
  156. "redirect URIs."),
  157. )
  158. parser.add_argument(
  159. "--additional_scopes",
  160. default=None,
  161. type=str,
  162. nargs="+",
  163. help="Additional scopes to apply when generating the refresh token.",
  164. )
  165. args = parser.parse_args()
  166. configured_scopes = [_SCOPE]
  167. if args.additional_scopes:
  168. configured_scopes.extend(args.additional_scopes)
  169. main(args.client_secrets_path, configured_scopes)