def get_authenticated_service(user): """ 让用户到google上进行认证,返回认证后的http服务 :param request: :return: """ storage = Storage(CredentialsModel, 'id', user, 'credential') credential = storage.get() if credential is None or credential.invalid is True: result = None else: SETTING_FILE = 'production' # 如果是VPS中运行,则不使用代理 if SETTING_FILE == 'production': http = httplib2.Http() http = credential.authorize(http) # 如果是本地运行,则使用代理,貌似使用socks5代理会出错,尝试使用http代理 if SETTING_FILE == 'local': myproxy = httplib2.ProxyInfo( proxy_type=httplib2.socks.PROXY_TYPE_HTTP, proxy_host='127.0.0.1', proxy_port=8118) http = httplib2.Http(proxy_info=myproxy) http = credential.authorize(http) service = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=http) result = service return result
def get_credentials(): """Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential. """ home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, '.credentials') if not os.path.exists(credential_dir): os.makedirs(credential_dir) credential_path = os.path.join(credential_dir, 'gmail-python-quickstart.json') store = Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatibility with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials
def get_credentials(options, cache_location=None): """ Get credentials object needed for constructing GooglePlayAPi. There are a coupe of ways to get the credentials: - using a service {'service': {'json': path-to-json.json}} or {'service': {'p12': path-to-p12.p12}} - using oauth {'oauth': {'json': path-to-client-secret-json.json}} or {'oauth': {'client-id': your-client-id, 'client-secret': your-client-secret} :param options: the authentication options :param cache_location: (optional) if using oauth it'll store the credentials in a file :return: the credentials object """ credentials = None flow = None scope = 'https://www.googleapis.com/auth/androidpublisher' redirect_uri = 'urn:ietf:wg:oauth:2.0:oob' if 'service' in options and 'json' in options['service']: credentials = ServiceAccountCredentials.from_json_keyfile_name( options['service']['json'], [scope]) if 'service' in options and 'p12' in options['service']: credentials = ServiceAccountCredentials.from_p12_keyfile( options['p12'], [scope]) if 'oauth' in options and 'json' in options['oauth']: flow = flow_from_clientsecrets( options['oauth']['json'], scope=scope, redirect_uri=redirect_uri) if 'oauth' in options is True and 'client-id' in options['oauth'] and 'client-secret' in options['oauth']: flow = OAuth2WebServerFlow( client_id=options['oauth']['client-id'], client_secret=options['oauth']['client-secret'], scope=scope, redirect_uri=redirect_uri) if flow is not None: if cache_location is None: cache_location = os.path.join(os.getenv("HOME"), ".gplay", "credentials.dat") storage = Storage(cache_location) credentials = storage.get() if credentials is None or credentials.invalid: credentials = tools.run_flow(flow, storage) if credentials is None: exit(ValueError('missing credentials')) return credentials