| skipped 74 lines |
75 | 75 | | |
76 | 76 | | import fmbox |
77 | 77 | | |
| 78 | + | def getGYBVersion(divider="\n"): |
| 79 | + | return ('Got Your Back %s~DIV~%s~DIV~%s - %s~DIV~Python %s.%s.%s %s-bit \ |
| 80 | + | %s~DIV~google-api-client %s~DIV~%s %s' % (__version__, __website__, __author__, __email__, |
| 81 | + | sys.version_info[0], sys.version_info[1], sys.version_info[2], |
| 82 | + | struct.calcsize('P')*8, sys.version_info[3], googleapiclient.__version__, platform.platform(), |
| 83 | + | platform.machine())).replace('~DIV~', divider) |
| 84 | + | |
| 85 | + | USER_AGENT = getGYBVersion(' | ') |
| 86 | + | # Override and wrap google_auth_httplib2 request methods so that the |
| 87 | + | # user-agent string is inserted into HTTP request headers. |
| 88 | + | def _request_with_user_agent(request_method): |
| 89 | + | """Inserts the user-agent header kwargs sent to a method.""" |
| 90 | + | def wrapped_request_method(self, *args, **kwargs): |
| 91 | + | if kwargs.get('headers') is not None: |
| 92 | + | if kwargs['headers'].get('user-agent'): |
| 93 | + | if USER_AGENT not in kwargs['headers']['user-agent']: |
| 94 | + | # Save the existing user-agent header and tack on the user-agent. |
| 95 | + | kwargs['headers']['user-agent'] = '%s %s' % (USER_AGENT, kwargs['headers']['user-agent']) |
| 96 | + | else: |
| 97 | + | kwargs['headers']['user-agent'] = USER_AGENT |
| 98 | + | else: |
| 99 | + | kwargs['headers'] = {'user-agent': USER_AGENT} |
| 100 | + | return request_method(self, *args, **kwargs) |
| 101 | + | return wrapped_request_method |
| 102 | + | |
| 103 | + | google_auth_httplib2.Request.__call__ = _request_with_user_agent( |
| 104 | + | google_auth_httplib2.Request.__call__) |
| 105 | + | google_auth_httplib2.AuthorizedHttp.request = _request_with_user_agent( |
| 106 | + | google_auth_httplib2.AuthorizedHttp.request) |
| 107 | + | |
78 | 108 | | def SetupOptionParser(argv): |
79 | 109 | | tls_choices = [] |
80 | 110 | | if getattr(ssl, 'TLSVersion', False): |
| skipped 155 lines |
236 | 266 | | continue |
237 | 267 | | systemErrorExit(4, str(e)) |
238 | 268 | | elif credentials is None or not credentials.valid: |
239 | | - | doRequestOAuth() |
| 269 | + | requestOAuthAccess() |
240 | 270 | | credentials = getOauth2TxtStorageCredentials() |
241 | 271 | | return credentials |
242 | 272 | | |
| skipped 175 lines |
418 | 448 | | if last_check_time > now_time-604800: |
419 | 449 | | return |
420 | 450 | | check_url = check_url + '/latest' # latest full release |
421 | | - | headers = {'Accept': 'application/vnd.github.v3.text+json'} |
| 451 | + | headers = {'Accept': 'application/vnd.github.v3.text+json', |
| 452 | + | 'User-Agent': getGYBVersion(' | ')} |
422 | 453 | | anonhttpc = _createHttpObj() |
423 | 454 | | try: |
424 | 455 | | (_, c) = anonhttpc.request(check_url, 'GET', headers=headers) |
| skipped 52 lines |
477 | 508 | | def buildGAPIObject(api): |
478 | 509 | | credentials = getValidOauth2TxtCredentials() |
479 | 510 | | httpc = google_auth_httplib2.AuthorizedHttp(credentials, _createHttpObj()) |
480 | | - | credentials.user_agent = getGYBVersion(' | ') |
481 | 511 | | if options.debug: |
482 | 512 | | extra_args['prettyPrint'] = True |
483 | 513 | | if os.path.isfile(os.path.join(getProgPath(), 'extra-args.txt')): |
| skipped 164 lines |
648 | 678 | | long_url, state = super(ShortURLFlow, self).authorization_url(**kwargs) |
649 | 679 | | simplehttp = _createHttpObj(timeout=10) |
650 | 680 | | url_shortnr = 'https://gyb-shortn.jaylee.us/create' |
651 | | - | headers = {'Content-Type': 'application/json', |
652 | | - | 'user-agent': getGYBVersion(divider=" | ")} |
| 681 | + | headers = {'Content-Type': 'application/json'} |
653 | 682 | | try: |
654 | 683 | | resp, content = simplehttp.request(url_shortnr, 'POST', '{"long_url": "%s"}' % long_url, headers=headers) |
655 | 684 | | except: |
| skipped 41 lines |
697 | 726 | | client_id = '297408095146-fug707qsjv4ikron0hugpevbrjhkmsk7.apps.googleusercontent.com' |
698 | 727 | | client_secret = 'qM3dP8f_4qedwzWQE1VR4zzU' |
699 | 728 | | credentials = _run_oauth_flow(client_id, client_secret, scope, 'online', login_hint) |
700 | | - | httpc = credentials.authorize(_createHttpObj()) |
701 | | - | return googleapiclient.discovery.build('cloudresourcemanager', 'v1', |
| 729 | + | httpc = google_auth_httplib2.AuthorizedHttp(credentials) |
| 730 | + | crm = googleapiclient.discovery.build('cloudresourcemanager', 'v1', |
702 | 731 | | http=httpc, cache_discovery=False, |
703 | 732 | | discoveryServiceUrl=googleapiclient.discovery.V2_DISCOVERY_URI) |
| 733 | + | return (crm, httpc) |
704 | 734 | | |
705 | 735 | | GYB_PROJECT_APIS = 'https://raw.githubusercontent.com/jay0lee/got-your-back/master/project-apis.txt?' |
706 | | - | def enableProjectAPIs(project_name, checkEnabled): |
707 | | - | httpc = _createHttpObj() |
| 736 | + | def enableProjectAPIs(project_name, checkEnabled, httpc): |
708 | 737 | | anonhttpc = _createHttpObj() |
709 | | - | s, c = anonhttpc.request(GYB_PROJECT_APIS, 'GET') |
| 738 | + | headers = {'User-Agent': getGYBVersion(' | ')} |
| 739 | + | s, c = anonhttpc.request(GYB_PROJECT_APIS, 'GET', headers=headers) |
710 | 740 | | if s.status < 200 or s.status > 299: |
711 | 741 | | print('ERROR: tried to retrieve %s but got %s' % (GYB_PROJECT_APIS, s.status)) |
712 | 742 | | sys.exit(0) |
| skipped 47 lines |
760 | 790 | | post_data = {'client_id': client_id, 'client_secret': client_secret, |
761 | 791 | | 'code': 'ThisIsAnInvalidCodeOnlyBeingUsedToTestIfClientAndSecretAreValid', |
762 | 792 | | 'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob', 'grant_type': 'authorization_code'} |
763 | | - | headers = {'Content-type': 'application/x-www-form-urlencoded'} |
| 793 | + | headers = {'Content-type': 'application/x-www-form-urlencoded', |
| 794 | + | 'User-Agent': getGYBVersion(' | ')} |
764 | 795 | | anonhttpc = _createHttpObj() |
765 | 796 | | _, content = anonhttpc.request(url, 'POST', urlencode(post_data), headers=headers) |
766 | 797 | | try: |
| skipped 71 lines |
838 | 869 | | print('ERROR: delete-projects action requires --email and a project --search argument') |
839 | 870 | | sys.exit(3) |
840 | 871 | | login_hint = getValidateLoginHint(login_hint) |
841 | | - | crm = getCRMService(login_hint) |
| 872 | + | crm, _ = getCRMService(login_hint) |
842 | 873 | | client_secrets_file = os.path.join(getProgPath(), 'client_secrets.json') |
843 | 874 | | if pfilter == 'current': |
844 | 875 | | cs_data = readFile(client_secrets_file, mode='rb', continueOnError=True, displayError=True, encoding=None) |
| skipped 35 lines |
880 | 911 | | sys.exit(5) |
881 | 912 | | login_hint = options.email |
882 | 913 | | login_domain = login_hint[login_hint.find('@')+1:] |
883 | | - | crm = getCRMService(login_hint) |
| 914 | + | crm, httpc = getCRMService(login_hint) |
884 | 915 | | project_id = 'gyb-project' |
885 | 916 | | for i in range(3): |
886 | 917 | | project_id += '-%s' % ''.join(random.choice(string.digits + string.ascii_lowercase) for i in range(3)) |
| skipped 70 lines |
957 | 988 | | print(status['error']) |
958 | 989 | | sys.exit(2) |
959 | 990 | | break |
960 | | - | enableProjectAPIs(project_name, False) |
961 | | - | iam = googleapiclient.discovery.build('iam', 'v1', http=_createHttpObj(), |
| 991 | + | enableProjectAPIs(project_name, False, httpc) |
| 992 | + | iam = googleapiclient.discovery.build('iam', 'v1', http=httpc, |
962 | 993 | | cache_discovery=False, |
963 | 994 | | discoveryServiceUrl=googleapiclient.discovery.V2_DISCOVERY_URI) |
964 | 995 | | print('Creating Service Account') |
| skipped 237 lines |
1202 | 1233 | | ('db_version', __db_schema_version__))) |
1203 | 1234 | | sqlconn.commit() |
1204 | 1235 | | |
1205 | | - | def getGYBVersion(divider="\n"): |
1206 | | - | return ('Got Your Back %s~DIV~%s~DIV~%s - %s~DIV~Python %s.%s.%s %s-bit \ |
1207 | | - | %s~DIV~%s %s' % (__version__, __website__, __author__, __email__, |
1208 | | - | sys.version_info[0], sys.version_info[1], sys.version_info[2], |
1209 | | - | struct.calcsize('P')*8, sys.version_info[3], platform.platform(), |
1210 | | - | platform.machine())).replace('~DIV~', divider) |
1211 | | - | |
1212 | 1236 | | def labelIdsToLabels(labelIds): |
1213 | 1237 | | global allLabelIds, gmail |
1214 | 1238 | | labels = list() |
| skipped 215 lines |
1430 | 1454 | | print('Path: %s' % getProgPath()) |
1431 | 1455 | | print(ssl.OPENSSL_VERSION) |
1432 | 1456 | | anonhttpc = _createHttpObj() |
1433 | | - | anonhttpc.request('https://www.googleapis.com') |
| 1457 | + | headers = {'User-Agent': getGYBVersion(' | ')} |
| 1458 | + | anonhttpc.request('https://www.googleapis.com', headers=headers) |
1434 | 1459 | | cipher_name, tls_ver, _ = anonhttpc.connections['https:www.googleapis.com'].sock.cipher() |
1435 | 1460 | | print('www.googleapis.com connects using %s %s' % (tls_ver, cipher_name)) |
1436 | 1461 | | sys.exit(0) |
| skipped 691 lines |