Setting HTTP-proxy for Google Analytics Reporting API

拥有回忆 提交于 2021-02-10 18:00:36

问题


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

  • setting proxy using httplib2shim
  • client api proxy
  • official repository for httplib2shim
  • google proxy support link
  • google-api-python-client repository

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.

来源:https://stackoverflow.com/questions/65873845/setting-http-proxy-for-google-analytics-reporting-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!