2

I have read the following links, which tell us how to set the proxy on Google APIs:

My problem is different from the other questions, since I am using ServiceAccountCredentials method from oauth2client.service_account package. The authentication to the API key is done with json file. (also ServiceAccountCredentials.from_json_keyfile_name method). Whenever I want to request to the google service I want to pass it from http proxy server, unfortunately there isn't any rich documentation for this purpose on the web (or maybe I couldn't find them).

ga_base_config.yml

PROXY:
  proxy_user: '<proxy_user>'
  proxy_pass: '<proxy_password>'
  proxy_host: '<proxy_host>'
  proxy_port: <port_no>

main.py

class GA:
    def __init__(self, root_dir):
        with open(os.path.join(root_dir, 'config/ga_base_config.yml'), 'r') as yaml_file:
            try:
                self.yaml_file = yaml.safe_load(yaml_file)
                print(self.yaml_file)
            except yaml.YAMLError as exc:
                print(exc)

        self.scopes = self.yaml_file['URL']['scope']
        self.proxy_info = self.yaml_file['PROXY']

        self.key_file_location = os.path.join(root_dir, self.yaml_file['KEY_FILE'])

    def initialize_analytics_reporting(self):
        """Initializes an Analytics Reporting API V4 service object.

        Returns:
          An authorized Analytics Reporting API V4 service object.
        """
        credentials = ServiceAccountCredentials.from_json_keyfile_name(
            self.key_file_location,
            self.scopes,
        )
        proxy = httplib2shim.Http(self.proxy_info)
        credentials.authorize(http=proxy)
        analytics = build(
            'analyticsreporting',
            'v4',
            credentials=credentials,
        )
        return analytics

I have tried the above code, but it doesn't work and I don't know how to pass Google Analytics API from HTTP proxy.

P.S. I am using python 3.8.

Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102

0 Answers0