python helper类来简化与googleapi的交互。基于python的google api客户端。

google-api-helper的Python项目详细描述


google api助手帮助简化对googleapi的访问,包括使用oauth2和工厂方法创建api服务的身份验证。也就是说,创建compute a p i服务并不坏,但仍需要一些代码:

importgoogleapiclient.discoveryfromoauth2client.service_accountimportServiceAccountCredentialscredentials=ServiceAccountCredentials.from_json_keyfile_name("service_account.json",["https://www.googleapis.com/auth/compute"])compute=googleapiclient.discovery.build('compute','v1',credentials=credentials)

使用google api helper,这是一个单行程序:

fromgoogleapiimportGoogleApicompute=GoogleApi.compute().with_service_account_file("service_account.json")

使用oauth2流也很简单

fromgoogleapiimportGoogleApicompute=GoogleApi.compute().with_oauth2_flow("client_secret.json")

python-google-api客户端还可以重试包含的服务器错误,但不限于速率限制。因此,您进行的每个api调用都需要实现指数退避。这是通过使用google api助手自动完成的。

fromgoogleapiimportGoogleApicompute=GoogleApi.compute().with_oauth2_flow("client_secret.json")# directly using the api service without retriescompute.service.instances().list(project="my-gcp-project",zone="europe-west1-d").execute()# wrapper including retries for rate limiting and server side errorscompute.instances().list(project="my-gcp-project",zone="europe-west1-d").execute()

安装

将google api helper添加到requirements.txt或模块需求中,或通过pip安装:

pip install google-api-helper

快速启动

在python代码中导入googleapi

fromgoogleapiimportGoogleApi

从工厂方法创建API或自行指定:

compute=GoogleApi.compute()drive=GoogleApi.drive()gmail=GoogleApi("gmail","v1",["https://mail.google.com/"])

使用OAuth2进行身份验证

# use Compute Engine or App Engine default credentialscompute.with_application_credentials()# use a service account to access a users drivedrive.with_service_account_file("service_account.json","test@example.com")# or run a oauth2 flow to ask the user for credentialsgmail.with_oauth2_flow("client_secret.json")

并调用api

instances=compute.instances().list(project="project-id",zone="europe-west1-d").execute()drive_files=drive.files().list(q="name='test'").execute()messages=gmail.users().messages().list(userId="me").execute()

构建和发布

python setup.py bdist_wheel --universal
python -m twine upload dist/*

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JPA。Eclipselink没有为mySQL提供密码,但它应该提供   我的Servlet和@FormDataParam存在java问题   java将什么作为上下文参数传递到文件I/O方法中?   如果两个值相同,java无法找到其中一个单选按钮   java在变量和方法名中使用下划线   JavaSpringMVC单线程安全?   klazz类的java Arraylist(反射Api)   java如何在数字字符串中查找最频繁的数字?   JavaAPI设计:使数据更易于阅读与强制更多API调用   JavaHadoopMapReduceforGoogleWebGraph   java无法启动gauge API:Runner意外退出   java如何在bluemix上使用ibm工作负载调度器?   拉取一年中某一周特定日期的所有日期   java为什么是我的角节点。js应用程序将图像上传到S3� 邮递员正确上传时的符号?   在不使用任何第三方jar的情况下将文件从本地传输到linux系统(java代码)   java将现有文件夹复制到Eclipse工作区中新创建的项目中   Java中的regex RegExp帮助   当使用“系统”外观时,Java组合框setSelectedItem会出现故障   JavaASM:在类的方法中获取局部变量名和值