gauthenticate.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 creates an OAuth 2.0 refresh token for the Google Ads API.
  16. This illustrates how to step through the OAuth 2.0 native / installed
  17. application flow.
  18. It is intended to be run from the command line and requires user input.
  19. """
  20. import argparse
  21. from google_auth_oauthlib.flow import InstalledAppFlow
  22. SCOPE = "https://www.googleapis.com/auth/adwords"
  23. def main(client_secrets_path, scopes):
  24. flow = InstalledAppFlow.from_client_secrets_file(
  25. client_secrets_path, scopes=scopes
  26. )
  27. flow.run_console()
  28. print("Access token: %s" % flow.credentials.token)
  29. print("Refresh token: %s" % flow.credentials.refresh_token)
  30. if __name__ == "__main__":
  31. parser = argparse.ArgumentParser(
  32. description="Generates OAuth 2.0 credentials with the specified "
  33. "client secrets file."
  34. )
  35. # The following argument(s) should be provided to run the example.
  36. parser.add_argument(
  37. "--client_secrets_path",
  38. required=True,
  39. help=(
  40. "Path to the client secrets JSON file from the "
  41. "Google Developers Console that contains your "
  42. "client ID and client secret."
  43. ),
  44. )
  45. parser.add_argument(
  46. "--additional_scopes",
  47. default=None,
  48. help=(
  49. "Additional scopes to apply when generating the "
  50. "refresh token. Each scope should be separated "
  51. "by a comma."
  52. ),
  53. )
  54. args = parser.parse_args()
  55. configured_scopes = [SCOPE]
  56. if args.additional_scopes:
  57. configured_scopes.extend(
  58. args.additional_scopes.replace(" ", "").split(",")
  59. )
  60. main(args.client_secrets_path, configured_scopes)