class GoogleAuth(object): google_app: typing.Optional[SocialApp] = None social_auth_token: typing.Optional[SocialToken] _credentials: typing.Optional[GoogleCredentials] = None def __init__( self, social_auth_token: typing.Optional[SocialToken] = None, ) -> None: self.social_auth_token = social_auth_token self.google_app = SocialApp.objects.filter(provider="google").first() def auth_token_expired(self): return (self.social_auth_token.expires_at is not None and self.social_auth_token.expires_at < timezone.now()) def init_credentials(self): """Build a `GoogleCredentials` object from the `SocialToken`/client IDs we have.""" if self._credentials is not None: return self._credentials = GoogleCredentials( self.social_auth_token.token, self.google_app.client_id, self.google_app.secret, self.social_auth_token.token_secret, self.social_auth_token.expires_at, GOOGLE_TOKEN_URI, "Stencila Hub Client", ) @property def credentials(self) -> typing.Optional[GoogleCredentials]: if self.social_auth_token is None: return None if self.google_app is None: return None self.check_and_refresh_token() return self._credentials def update_social_auth_token(self): self.social_auth_token.token = self._credentials.access_token self.social_auth_token.expires_at = timezone.make_aware( self._credentials.token_expiry, timezone.utc) self.social_auth_token.save() def check_and_refresh_token(self): """Refresh the credentials from Google, if expired (according to us).""" self.init_credentials() if not self.auth_token_expired(): return self.social_auth_token.token http = self._credentials.authorize(httplib2.Http()) self._credentials.refresh(http) self.update_social_auth_token() return self.social_auth_token.token
def refresh(client_id, client_secret, refresh_token): cred = GoogleCredentials(None, client_id, client_secret, refresh_token, None, "https://accounts.google.com/o/oauth2/token", None) http = cred.authorize(httplib2.Http()) cred.refresh(http) return cred.access_token
def get_user_google_token( user: User, ) -> Tuple[Optional[SocialToken], Optional[SocialApp]]: """ Get a Google `SocialToken` for the user. If necessary will refresh the OAuth2 access token and update it in the database so that the refresh does not need to be done again within the next hour (at time of writing the expiry time for tokens). In most contexts that this function is used the Google `SocialApp` is also needed (e.g. for it's client_id etc) so we return that too. To avoid exceptions during development where there might not be a Google `SocialApp` we return None. """ token = get_user_social_token(user, Provider.google) try: app = SocialApp.objects.get(provider=Provider.google.name) except SocialApp.DoesNotExist: app = None if token is None: return None, app # If the token has not expired just return it if token.expires_at is None or token.expires_at > timezone.now() - timezone.timedelta( seconds=90 ): return token, app # The folowing are all required for a token refresh so if any # are missing, and the token has expired, return no token. if not (token.token and token.token_secret and token.expires_at): return None, app # Refresh the token credentials = GoogleCredentials( access_token=token.token, client_id=app.client_id, client_secret=app.secret, refresh_token=token.token_secret, token_expiry=token.expires_at, token_uri="https://accounts.google.com/o/oauth2/token", user_agent="Stencila Hub Client", ) credentials.refresh(http=transport.get_http_object()) info = credentials.get_access_token() # Save the new access token and expiry time token.token = info.access_token token.expires_at = timezone.now() + timezone.timedelta(seconds=info.expires_in) token.save() return token, app
def getDriveService(user_agent): with open(TOKEN) as f: creds = json.load(f) credentials = GoogleCredentials(None,creds["client_id"],creds["client_secret"], creds["refresh_token"],None,"https://accounts.google.com/o/oauth2/token",user_agent) http = credentials.authorize(Http()) credentials.refresh(http) drive_service = build('drive', 'v3', http) return drive_service
def _get_credentials(self) -> dict: """ Authenticates with google fit and returns a dictionary with the most recent valid credentials """ online_credentials = self.parameter_manager.get_multiple('/google_fit/') credentials = GoogleCredentials(**online_credentials, token_expiry=None, user_agent=None) http = credentials.authorize(httplib2.Http()) credentials.refresh(http) credentials_dict = json.loads(credentials.to_json()) self.access_token = credentials_dict.get("access_token") self._store_credentials_online(credentials_dict) return credentials_dict
def configure_youtube(): cred = GoogleCredentials(None, os.environ.get('GOOGLE_CLIENT_ID'), os.environ.get('GOOGLE_CLIENT_SECRET'), os.environ.get('GOOGLE_REFRESH_TOKEN'), None, "https://accounts.google.com/o/oauth2/token", '') http = cred.authorize(httplib2.Http()) cred.refresh(http) api_service_name = "youtube" api_version = "v3" return googleapiclient.discovery.build(api_service_name, api_version, credentials=cred, cache_discovery=False)
def initialize_credentials(self, config): if 'oauth_credentials' in config: credentials = GoogleCredentials( access_token=config['oauth_credentials']['access_token'], refresh_token=config['oauth_credentials']['refresh_token'], client_id=config['oauth_credentials']['client_id'], client_secret=config['oauth_credentials']['client_secret'], token_expiry=None, # let the library refresh the token if it is expired token_uri="https://accounts.google.com/o/oauth2/token", user_agent="tap-google-analytics (via singer.io)" ) logging.info("refreshing access token") credentials.refresh(httplib2.Http()) logging.info("refreshed successfully") return credentials else: return ServiceAccountCredentials.from_json_keyfile_dict(config['client_secrets'], SCOPES)
def initialize_analyticsreporting(self): if not self.is_api_ready: credentials = GoogleCredentials( access_token=self.access_token, client_id=self.client_id, client_secret=self.client_secret, refresh_token=self.refresh_token, token_expiry=None, token_uri="https://accounts.google.com/o/oauth2/token", user_agent=None, revoke_uri=GOOGLE_REVOKE_URI, ) http = credentials.authorize(httplib2.Http()) credentials.refresh(http) self._service = build(serviceName="webmasters", version="v3", credentials=credentials, cache_discovery=False)
import httplib2 from googleapiclient.errors import HttpError from oauth2client.client import GoogleCredentials from reddityoutubehaikus import RedditYoutubeHaikus from youtubehelper import YoutubeHelper creds = GoogleCredentials(environ['YOUTUBE_ACCESS_TOKEN'], environ['YOUTUBE_CLIENT_ID'], environ['YOUTUBE_CLIENT_SECRET'], environ['YOUTUBE_REFRESH_TOKEN'], None, "https://www.googleapis.com/oauth2/v4/token", 'Youtube Haikus') http = creds.authorize(httplib2.Http()) creds.refresh(http) youtube_helper = YoutubeHelper(creds) time_periods = [{ 'interval': 'week', 'title': 'Top of the week' }, { 'interval': 'month', 'title': 'Top of the month' }, { 'interval': 'year', 'title': 'Top of the year' }, { 'interval': 'all', 'title': 'Top ever' }]
class APIClient: def __init__(self, config): self.credentials = GoogleCredentials( None, client_id=config.get('client_id'), client_secret=config.get('client_secret'), refresh_token=config.get('refresh_token'), token_expiry=None, token_uri="https://accounts.google.com/o/oauth2/token", user_agent=config.get('user_agent')) self.refresh_credentials() def refresh_credentials(self): http = self.credentials.authorize(Http()) self.credentials.refresh(http) def get_report(self, profile_id, report_id): service = build('dfareporting', 'v3.0', credentials=self.credentials) logger.info("Running report {} for profile {}...".format( report_id, profile_id)) run = service.reports().run(profileId=profile_id, reportId=report_id).execute() file_id = run.get('id') running = True while running: f = service.reports().files().get(profileId=profile_id, reportId=report_id, fileId=file_id).execute() if f.get('status') == 'PROCESSING': logger.info("> Report is still processing, retry in 5s.") sleep(5) else: running = False logger.info("Completed with status '{}'.".format(f.get('status'))) file_value = service.reports().files().get_media( profileId=profile_id, reportId=report_id, fileId=file_id).execute().decode('utf-8') # the data comes in with some garbage on the first few, and # last, lines. remove it split = file_value.split('\n') header_lines = split[0:9] data_lines = split[9:] data_lines = data_lines[:-2] generated_time = datetime.strptime( # 2/16/18 1:42 PM header_lines[2].split(',')[1], '%m/%d/%y %I:%M %p') # 2/1/18 - 2/16/18 time_range = header_lines[5].split(',')[1] output = [] for index, line in enumerate(data_lines): if index == 0: output.append('{},{},{}'.format(line, 'generated_time', 'time_range')) else: output.append('{},{},{}'.format(line, generated_time, time_range)) return ( file_id, '\n'.join(output).encode('utf-8'), )