def make_secure_channel(credentials, user_agent, host, extra_options=()): """Makes a secure channel for an RPC service. Uses / depends on gRPC. :type credentials: :class:`google.auth.credentials.Credentials` :param credentials: The OAuth2 Credentials to use for creating access tokens. :type user_agent: str :param user_agent: The user agent to be used with API requests. :type host: str :param host: The host for the service. :type extra_options: tuple :param extra_options: (Optional) Extra gRPC options used when creating the channel. :rtype: :class:`grpc._channel.Channel` :returns: gRPC secure channel with credentials attached. """ target = '%s:%d' % (host, http_client.HTTPS_PORT) http_request = google_auth_httplib2.Request(http=httplib2.Http()) user_agent_option = ('grpc.primary_user_agent', user_agent) options = (user_agent_option, ) + extra_options return google.auth.transport.grpc.secure_authorized_channel( credentials, http_request, target, options=options)
def make_secure_channel(credentials, user_agent, host): """Makes a secure channel for an RPC service. Uses / depends on gRPC. :type credentials: :class:`google.auth.credentials.Credentials` :param credentials: The OAuth2 Credentials to use for creating access tokens. :type user_agent: str :param user_agent: The user agent to be used with API requests. :type host: str :param host: The host for the service. :rtype: :class:`grpc._channel.Channel` :returns: gRPC secure channel with credentials attached. """ # ssl_channel_credentials() loads root certificates from # `grpc/_adapter/credentials/roots.pem`. transport_creds = grpc.ssl_channel_credentials() http = httplib2.Http() custom_metadata_plugin = AuthMetadataPlugin( credentials, google_auth_httplib2.Request(http=http)) auth_creds = grpc.metadata_call_credentials(custom_metadata_plugin, name='google_creds') channel_creds = grpc.composite_channel_credentials(transport_creds, auth_creds) target = '%s:%d' % (host, http_client.HTTPS_PORT) channel_args = (('grpc.primary_user_agent', user_agent), ) return grpc.secure_channel(target, channel_creds, options=channel_args)
def send_reports(): if request.headers.get('X-Appengine-Cron') != 'true': abort(404) already_parsed_endpoint_ids = fetch_all_endpoint_ids() print(already_parsed_endpoint_ids) with open('oauth2.txt') as f: cdata = json.load(f) httpc = httplib2.Http() req = google_auth_httplib2.Request(httpc) creds = google.oauth2.credentials.Credentials.from_authorized_user_file('oauth2.txt') creds.token = cdata.get('token', cdata.get('auth_token', '')) creds._id_token = cdata.get('id_token_jwt', cdata.get('id_token', None)) token_expiry = cdata.get('token_expiry', '1970-01-01T00:00:01Z') creds.expiry = datetime.datetime.strptime(token_expiry, '%Y-%m-%dT%H:%M:%SZ') creds.refresh(req) httpc = google_auth_httplib2.AuthorizedHttp(creds, httpc) rep = googleapiclient.discovery.build('admin', 'reports_v1', http=httpc, cache_discovery=False) gmail = googleapiclient.discovery.build('gmail', 'v1', http=httpc, cache_discovery=False) cal = googleapiclient.discovery.build('calendar', 'v3', http=httpc, cache_discovery=False) now = datetime.datetime.utcnow() two_days_ago = now - datetime.timedelta(days=2) two_days_ago = two_days_ago.isoformat(timespec='seconds') + 'Z' min_age = now - datetime.timedelta(minutes=MINIMUM_AGE_MINUTES) min_age = min_age.isoformat(timespec='seconds') + 'Z' print(f'Start time: {two_days_ago} End time: {min_age}') response = gapi.call_pages(rep.activities(), 'list', applicationName='meet', userKey='all', eventName='call_ended', startTime=two_days_ago, endTime=min_age) meetings = parse_report(response, cal, ignore_endpoint_ids=already_parsed_endpoint_ids) draw_meetings(gmail, meetings) return 'all done!'
def test_timeout(self): url = 'http://example.com' http = MockHttp(responses=[MockResponse()]) request = google_auth_httplib2.Request(http) request(url=url, method='GET', timeout=5) assert http.requests[0] == ('GET', url, None, None, {})
def refresh_credentials(credentials): # Refresh must use a new http instance, as the one associated with the # credentials could be a AuthorizedHttp or an oauth2client-decorated # Http instance which would cause a weird recursive loop of refreshing # and likely tear a hole in spacetime. refresh_http = httplib2.Http() if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials): request = google_auth_httplib2.Request(refresh_http) return credentials.refresh(request) else: return credentials.refresh(refresh_http)
def GoogleAuthRequest(http=None): """A Request object for google-auth library. Args: http: httplib2.Http client object configured with all the required settings for gcloud. Returns: A http request which implements google.auth.transport.Request and uses gcloud's http object in the core. """ return google_auth_httplib2.Request(http or Http())
def test_timeout(self): url = "http://example.com" http = MockHttp(responses=[MockResponse()]) request = google_auth_httplib2.Request(http) request(url=url, method="GET", timeout=5) assert http.requests[0] == ( "GET", url, None, None, httplib2.DEFAULT_MAX_REDIRECTS, None, )
def refresh_google_credentials(): users = GoogleCalendarUser.query.all() count = 0 for user in users: creds_dict = get_credentials_dict(user) credentials = Credentials(**creds_dict) rq = google_auth_httplib2.Request(httplib2.Http()) credentials.refresh(rq) user.auth_token = credentials.token db.session.add(user) db.session.commit() count += 1 print("Updated auth token for ", count, " GoogleCalendarUsers.")
def make_request(self): http = httplib2.Http() return google_auth_httplib2.Request(http)
def _request_factory(): http = httplib2.Http() return google_auth_httplib2.Request(http)