def authenticate(api_name): """ authenticates using OAUTH2 with the given API. """ if not os.path.isfile(credentials_file(api_name)): # if there is no credentials: run the flow. flow = flow_from_clientsecrets(secret_file(api_name), scope=get_scope(api_name)) parser = argparse.ArgumentParser(parents=[tools.argparser]) flags = parser.parse_args() # override default port with a random free port to avoid issues when # a second flow is initiated using the same port. override = Flags() # convert argparse.Namespace to dict to make it writable. override.__dict__ = flags.__dict__ # make sure we have a unique port. override.auth_host_port = [free_port()] # avoid having logging disabled. override.logging_level = 'INFO' storage = Storage(credentials_file(api_name)) tools.run_flow(flow, storage, flags) token = Storage(credentials_file(api_name)).get() if token.access_token_expired: token.refresh(httplib2.Http()) return token.access_token
def g_yetki(): # Kişisel bilgilei alır kimlik_bilgileri = Storage(G_DRIVE_TOKEN_DOSYASI).get() # httplib2.Http objesi oluşturur ve kişisel bilgilerinizle yetkilendirir. http = httplib2.Http() kimlik_bilgileri.refresh(http) referans = kimlik_bilgileri.authorize(http) return build("drive", "v3", http=referans, cache_discovery=False)
def get_service(storage_file): credentials = Storage(storage_file).get() if not credentials: raise Exception('credentials are invalid. Please authorize first.') http = Http() if credentials.access_token_expired: credentials.refresh(http) return discovery.build(serviceName='gmail', version='v1', http=credentials.authorize(http))
def yetkilendir( ): # fiziksel olarak dosya bağımlılığı yoktur ve drive_erisim.json oluşturur if not os.path.exists(G_DRIVE_TOKEN_DOSYASI): print('''\ndrive_erisim.json Bulunamadı.. Lütfen yetkilendirmenizi tamamlayınız..\n''') token_yarat() # Kişisel bilgilei alır kimlik_bilgileri = Storage(G_DRIVE_TOKEN_DOSYASI).get() # httplib2.Http objesi oluşturur ve kişisel bilgilerinizle yetkilendirir. http = httplib2.Http() kimlik_bilgileri.refresh(http) referans = kimlik_bilgileri.authorize(http) return build("drive", "v3", http=referans, cache_discovery=False)
def g_yetki(): if not os.path.exists(AYARLAR): print(erisim_ver()) sys.exit() if not os.path.exists(G_DRIVE_TOKEN_DOSYASI): print("\t[!] Önce Yetkilendirme Yapmalısınız...\n\n") print(kod_al()) print(token_olustur(input('\n\nLütfen Kodu Giriniz.. : '))) # Kişisel bilgilei alır kimlik_bilgileri = Storage(G_DRIVE_TOKEN_DOSYASI).get() # httplib2.Http objesi oluşturur ve kişisel bilgilerinizle yetkilendirir. http = httplib2.Http() kimlik_bilgileri.refresh(http) referans = kimlik_bilgileri.authorize(http) return build("drive", "v3", http=referans, cache_discovery=False)
def OAuth2Login(client_secrets, email, pswd, app_path): global auth_code scope = 'https://picasaweb.google.com/data/' user_agent = 'PhotoAlbum' cred_file = 'credentials.txt' #we already have the credentials stored in the file system if cred_file in os.listdir(app_path): credentials = Storage(app_path + cred_file).get() else: #we have to get the credentials from google flow = flow_from_clientsecrets(client_secrets, scope=scope, redirect_uri='http://localhost:9999') #start a thread to listen for the auth code listen_thread = threading.Thread(target = Listen) listen_thread.start() uri = flow.step1_get_authorize_url() webbrowser.open(uri) #wait for the thread to finish so we have the auth code listen_thread.join() code = auth_code.split('code=')[1].split()[0] #print 'The code is: {0}'.format(code) credentials = flow.step2_exchange(code) Storage(app_path + cred_file).put(credentials) if (credentials.token_expiry - datetime.datetime.utcnow()) < datetime.timedelta(minutes=5): http = httplib2.Http() http = credentials.authorize(http) credentials.refresh(http) gd_client = gdata.photos.service.PhotosService(source=user_agent, email=email, additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token}) return gd_client