我成功完成了此处的快速入门演示:
https://developers.google.com/drive/api/v3/quickstart/python
我希望更进一步,开始上传文件和文件夹。为此,我将范围替换为: SCOPES = ['https://www.googleapis.com/auth/drive']
我也将执行替换为
national_parks = ['Yellowstone', 'Rocky Mountain', 'Yosemite']
for national_park in national_parks:
file_metadata = {'name': national_park,
'mimeType': 'application/vnd.google-apps.folder'
# 'parents': []
}
service.files().create(body=file_metadata).execute()
Run Code Online (Sandbox Code Playgroud)
然后我收到一个错误: googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files?alt=json returned "Insufficient Permission: Request had invalidauthenticationscopes.">
请帮助
python google-api google-drive-api google-oauth google-api-python-client
我正在尝试将 google.oauth2、google_auth_oauthlib.flow 和 googleapiclient.discovery 用于我的网络应用程序。但我收到这些错误:
Import "google.auth.transport.requests" could not be resolved
Import "google.oauth2.credentials" could not be resolved
Import "googleapiclient.discovery" could not be resolved
Run Code Online (Sandbox Code Playgroud)
输出为: from google.auth.transport.requests import Request ModuleNotFoundError: No module named 'google' 这是我的 pip 列表:
google 3.0.0
google-api-core 2.8.2
google-api-python-client 2.51.0
google-auth 2.8.0
google-auth-httplib2 0.1.0
google-auth-oauthlib 0.5.2
googleapis-common-protos 1.56.3
gspread 5.4.0
httplib2 0.20.4
oauth2client 4.1.3
oauthlib 3.2.0
requests 2.28.0
requests-oauthlib 1.3.1
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我会收到这些错误。我希望你能帮助我。
python google-oauth google-api-python-client visual-studio-code
我在App Engine上使用Google的python API客户端库在Big Query中运行大量查询以生成实时分析.调用大约需要两秒钟,并且有五个查询,这太长了,所以我研究了加快速度的方法,并认为异步运行查询将是一个可靠的改进.我的想法是,我可以同时插入五个查询,谷歌会做一些魔法同时运行它们,然后jobs.getQueryResults(jobId)用来获取每个作业的结果.我决定通过计算两个异步查询的执行时间并将其与同步运行的查询进行比较来验证理论.结果:
getQueryResults())这只相差0.68秒.因此,尽管异步查询的速度更快,他们没有实现的谷歌平行魔术的目标以减少总执行时间.所以第一个问题:并行魔术的期望是否正确?即使不是,我特别感兴趣的是谷歌的说法
异步查询通常在查询完成之前立即返回响应.
大约半秒插入查询不符合我的'立即'定义!我想乔丹或Big Query团队中的其他人将是唯一可以回答此事的人,但我欢迎任何答案!
编辑说明:
根据Mikhail Berlyant的建议,我收集了creationTime,startTime并endTime从工作回复中发现:
creationTime至startTime:462ms,387ms(查询1和2的时间)startTime至endTime:744毫秒,1005毫秒虽然我不确定这是否会给故事增添任何内容,因为这是发布insert()和完成电话之间的时间,我很想知道.
您可以在BigQuery中同时运行多个作业
代码:
对于它的价值,我在本地和生产App Engine上进行了测试.本地速度减慢了约2-3倍,但复制了结果.在我的研究中,我也发现了分区表,我希望我之前知道(这可能最终成为我的解决方案),但这个问题依然存在.这是我的代码.我省略了实际的SQL,因为它们在这种情况下无关紧要:
def test_sync(self, request):
t0 = time.time()
request = bigquery.jobs()
data = { 'query': (sql) }
response = request.query(projectId=project_id, body=data).execute()
t1 = time.time() …Run Code Online (Sandbox Code Playgroud) python google-app-engine asynchronous google-bigquery google-api-python-client
这是一段代码:
import ast
from oauth2client import file, client, tools
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build
from apiclient import discovery
SCOPE = 'https://www.googleapis.com/auth/spreadsheets'
CREDJSON = "some-cred-file-downloaded-from-dev-console.json"
def google_credentials(jsoncred=CREDJSON, scope=SCOPE):
return ServiceAccountCredentials.from_json_keyfile_name(jsoncred, scope)
def csv_arrays(creds, key):
(SHEETS, sheets) = sheets_fetch(creds, key)
wks = sheets
def create_filename(arg):
filename = key.prefix + "_" + arg.get("properties, {}).get("title","Sheet1")
filename = filename.replace(" ", "_")
print (filename)
return filename
return [(create_filename(ws), ast.literal_eval(repr(SHEETS.spreadsheets().values().get(spreadsheetId=key.key, range=(ws.get("properties", {}).get("title", "Sheet1"))).execute().get('values',[])))) for ws in wks] #.decode("utf-8")
def sheets_fetch(creds, …Run Code Online (Sandbox Code Playgroud) 我已经在我的Django项目中为Google OAuth2 实现了python-social-auth库,并能够成功使用它登录用户。该库将access_token收到的信息存储在Google OAuth2流的响应中。
我的问题是:使用google-api-python-client似乎依赖于创建和授权credentials对象,然后使用它来构建API,service如下所示:
...
# send user to Google consent URL, get auth_code in response
credentials = flow.step2_exchange(auth_code)
http_auth = credentials.authorize(httplib2.Http())
from apiclient.discovery import build
service = build('gmail', 'v1', http=http_auth)
# use service for API calls...
Run Code Online (Sandbox Code Playgroud)
由于我是从access_tokenpython-social-auth提供的开始,因此如何service为未来的API调用创建和授权API客户端?
编辑:澄清一下,上面的代码来自Google提供的示例。
python oauth2client google-api-python-client python-social-auth google-oauth2
我正在尝试使用 Google API Python 客户端库启动 Google Compute 实例。这是为了让一个便宜的实例(在单个内核上运行)可以定期启动和停止一个更昂贵的实例(具有多个内核),以降低成本。
我已经成功安装了不同的组件并运行了 Google 的示例脚本create_instance.py(它创建了一个实例,运行了一个启动脚本,然后删除了该实例)。检查 Compute Engine API 的 PyDoc 参考,并交叉参考 create_instance.py 示例中其他实例()函数的工作方式,我希望启动实例命令是:
python compute.instances().start(project=*, zone=*, instance=*).execute()
Run Code Online (Sandbox Code Playgroud)
上面的命令给了我错误“在 '('. at line:1 char:34”之后需要一个表达式 - 这是第一个括号。
一种。我做错了什么?
湾 将 Google API 与 Python 结合使用是从其他实例以编程方式启动实例的好方法吗?
我正在使用Google People API来访问我的联系人。
我在Google Developers Console中激活了它,并创建了一个项目,一个服务帐户(以结尾....iam.gserviceaccount.com)和一个以JSON格式存储的身份验证密钥。
当我访问联系人时,似乎使用了我的服务帐户地址而不是我的Google帐户的联系人,从而导致列表为空。
如何告诉API使用我的帐户而不是服务帐户?
这是我到目前为止的代码:
from google.oauth2 import service_account
from googleapiclient.discovery import build
# pip install google-auth google-auth-httplib2 google-api-python-client
SCOPES = ['https://www.googleapis.com/auth/contacts.readonly']
KEY = '~/private.json'
credentials = service_account.Credentials.from_service_account_file(
KEY, scopes=SCOPES)
service = build(
serviceName='people', version='v1', credentials=credentials)
connections = service.people().connections().list(
resourceName='people/me', personFields='names').execute()
print(connections)
# result: {}
Run Code Online (Sandbox Code Playgroud) python google-api oauth-2.0 google-api-python-client google-people
我想从工作表中读取写入数据,读取工作正常,但写入却没有。我使用了文档中提到的所有范围:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append
data_writer(1,1,1)
Run Code Online (Sandbox Code Playgroud)
码:
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'+"https://www.googleapis.com/auth/drive.file"+"https://www.googleapis.com/auth/drive"
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = apiclient.discovery.build('sheets', 'v4', http=creds.authorize(Http()))
# Call the Sheets API
SPREADSHEET_ID = '1JwVOqtUCWBMm_O6esIb-9J4TgqAmMIdYm9sf5y-A7EM'
RANGE_NAME = 'Jokes!A:C'
# How the input data should be interpreted.
value_input_option = 'USER_ENTERED'
# How …Run Code Online (Sandbox Code Playgroud) python google-api google-oauth google-api-python-client anaconda
在使用 Google Drive API 获取文件列表时,我试图找到文件的路径。
现在,我能够获取文件属性(目前只获取校验和、id、名称和 mimeType):
results = globalShares.service.files().list(pageSize=1000,corpora='user',fields='nextPageToken, files(md5Checksum, id, name, mimeType)').execute()
items = results.get('files',[])
nextPageToken = results.get('nextPageToken',False)
for file in items:
print("===========================================================")
pp.pprint(file)
print(str(len(items)))
print(nextPageToken)
Run Code Online (Sandbox Code Playgroud)
列表文档(提供给 list() 方法的参数)
文件文档(每个文件返回的属性)
我尝试在 python 中使用 googledrive api v3 来使用官方 google 指令中的代码更新 googledrive 上的文件。
但我收到一个错误:
资源主体包括不可直接写入的字段。
怎么解决呢?
这是我尝试使用的代码:
try:
# First retrieve the file from the API.
file = service.files().get(fileId='id_file_in_google_drive').execute()
# File's new metadata.
file['title'] = 'new_title'
file['description'] = 'new_description'
file['mimeType'] = 'application/pdf'
# File's new content.
media_body = MediaFileUpload(
'/home/my_file.pdf',
mimetype='application/pdf',
resumable=True)
# Send the request to the API.
updated_file = service.files().update(
fileId='id_file_in_google_drive',
body=file,
media_body=media_body).execute()
return updated_file
except errors:
print('An error occurred: %s')
return None
Run Code Online (Sandbox Code Playgroud) python ×9
google-api ×5
google-oauth ×3
anaconda ×1
asynchronous ×1
oauth-2.0 ×1
oauth2client ×1