标签: google-api-python-client

Python bigquery - ImportError:没有名为google.apputils的模块

我试图使用谷歌bigquery python库,但每当我运行时,import bq我得到以下错误;

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-21-923a4eec0450> in <module>()
----> 1 import bq

/Users/tahirfayyaz/anaconda/python.app/Contents/lib/python2.7/site-packages/bq.py in      <module>()
     31 import oauth2client.tools
     32 
---> 33 from google.apputils import app
     34 from google.apputils import appcommands
     35 import gflags as flags

ImportError: No module named google.apputils
Run Code Online (Sandbox Code Playgroud)

我已安装甚至升级了google-apputils,但我仍然遇到此错误.

python import google-bigquery google-api-python-client

6
推荐指数
1
解决办法
3452
查看次数

如何将单词嵌入向量组合到一个向量中?

我完全了解单词嵌入(skip-gram,CBOW)的含义和方法.我知道,谷歌有一个word2vector API,通过获取单词可以生成向量.但我的问题是这样的:我们有一个条款,包括主题,对象,动词......每个单词以前都是由Google API嵌入的,现在是"我们如何将这些向量组合在一起创建一个等于条款?" 例子:条款:V ="狗咬人"在谷歌嵌入后,我们有V1,V2,V3,每个人都映射到狗,咬人,男人.我们知道:V = V1 + V2 + V3我们如何提供V?如果你通过一个真实载体的例子来解释它,我将不胜感激.

nlp information-retrieval google-api-python-client word2vec word-embedding

6
推荐指数
2
解决办法
3640
查看次数

适用于 python2.7 的 Google Sheets API --&gt;“无效的 JSON 负载。根元素必须是消息”

我已经为这个错误苦苦挣扎了好几个星期,并尝试了之前发布的与 Google Sheets 的 Python API 相关的问题的解决方案。

当我通过适用于 python 的 Google Sheets API 向电子表格发出“写入”请求时,我不断收到错误。该错误表明我正在提交无效的 JSON,但我已经针对交互式测试窗口(Google API Explorer)测试了 JSON 结构,并且来自那里的请求正确更新了我的工作表。

代码如下

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import datetime
import json

# Call the Sheets API
SPREADSHEET_ID =  #mySheetID
RANGE_NAME = '2018_Raw Data!A3:A367'

months = { 0:"Jan", 1:"Feb",2:"Mar",4:"Apr",5:"May",6:"Jun",7:"Jul",8:"Aug",9:"Sep",10:"Oct",11:"Nov",12:"Dec"}


now = datetime.datetime.now()
date = str(now.day) +"-"+ months[now.month] + "-"+str(now.year)
day_of_year = now.timetuple().tm_yday
myRow = day_of_year+2

print (date)
print (myRow)


BWRange= …
Run Code Online (Sandbox Code Playgroud)

python json google-api-python-client google-sheets-api

6
推荐指数
1
解决办法
4872
查看次数

从 Python 在 Google Colab 中生成 Google API access_token

我需要从 Google Colab 内的 Python 生成access_token一些特定范围的文件['https://www.googleapis.com/auth/bigquery', 'https://www.googleapis.com/auth/cloud-platform']。

默认情况下,Google Colab 在colabtools内部提供了一个功能来对用户进行身份验证。

from google.colab import auth
auth.authenticate_user()
print('Authenticated')
Run Code Online (Sandbox Code Playgroud)

但生成的凭证文件包含固定范围的列表,不包括我需要的范围。

import os
with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as f:
  print(f.read())
Run Code Online (Sandbox Code Playgroud)
...
"scopes": [
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/appengine.admin",
    "https://www.googleapis.com/auth/accounts.reauth",
    "https://www.googleapis.com/auth/cloud-platform",
    "https://www.googleapis.com/auth/compute",
    "https://www.googleapis.com/auth/userinfo.email"
  ],
...
Run Code Online (Sandbox Code Playgroud)

我尝试使用google.auth.Credentials类和google.oauth2.Credentialsgoogle-auth类通过库生成有效的凭据令牌。我可以生成类的有效实例,但是当我检查令牌时,它是“无”。enter code here

有没有办法access_token通过 Python 生成对 Google API 有效的自定义范围?

谢谢!

google-api access-token oauth-2.0 google-api-python-client google-colaboratory

6
推荐指数
1
解决办法
2809
查看次数

我可以使用 googleapiclient python 客户端复制 `create-with-container` gcloud sdk 命令吗

我目前在 gcloud 容器存储库上有一个 docker 容器,我想启动它的一个实例以运行计算、保存结果并关闭:

gcloud compute instances create-with-container test-1 \
--machine-type=n1-standard-4 \
--boot-disk-size=20GB \
--container-image=eu.gcr.io/<container-link> \
--container-env=GCLOUD_INPUT_FILENAME=file.txt \
--container-env=GCLOUD_PROJECT=project-name
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够使用 Web 界面(flask)启动这些实例,这意味着我想使用 googleapiclient ( python)来创建和管理这些实例:

看起来您可以使用发现 api 创建实例创建顺序:

gcloud compute instances create-with-container test-1 \
--machine-type=n1-standard-4 \
--boot-disk-size=20GB \
--container-image=eu.gcr.io/<container-link> \
--container-env=GCLOUD_INPUT_FILENAME=file.txt \
--container-env=GCLOUD_PROJECT=project-name
Run Code Online (Sandbox Code Playgroud)

但看起来不可能模拟create-with-containergcloud sdk 命令,尽管您可以将“machineImage”作为创建请求的一部分传递。

可以创建一个计算实例“with-container”而不使用subprocess调用 gcloud sdk

或者

我可以将create-with-container实例转换为机器映像然后使用吗googleapi client?

google-api-python-client google-cloud-platform gcloud gcloud-compute

6
推荐指数
1
解决办法
520
查看次数

如何修复:项目中未使用 Google Sheets API

我想构建一个问卷调查线聊天机器人并将答案传输到 Google 表格。\n这是我的代码:\n\'\'\'\nimport os

\n
from flask import Flask, request, abort\n\nfrom linebot import (\n    LineBotApi, WebhookHandler\n)\nfrom linebot.exceptions import (\n    InvalidSignatureError\n)\nfrom linebot.models import (\n    MessageEvent, TextMessage, TextSendMessage,\n)\nfrom oauth2client.service_account import ServiceAccountCredentials \nimport gspread\nfrom datetime import datetime, date, timedelta, time\nimport time\n\n\ngsp_scopes = [\'https://spreadsheets.google.com/feeds\']\n\nSPREAD_SHEETS_KEY = os.environ.get(\'SPREAD_SHEETS_KEY\')\ncredential_file_path = \'credentials.json\'\ndef auth_gsp_client(file_path, scopes):\n    credentials = ServiceAccountCredentials.from_json_keyfile_name(file_path, scopes)\n    return gspread.authorize(credentials)\n\ndef records(A, B, C, D, E):\n    gsp_client = auth_gsp_client(credential_file_path, gsp_scopes)\n    worksheet = gsp_client.open_by_key(SPREAD_SHEETS_KEY).sheet1\n    worksheet.insert_row([A, B, C, D, E], 2)\n    return True\n\napp = Flask(__name__)\n\nLINE_CHANNEL_ACCESS_TOKEN = os.environ.get(\'LINE_CHANNEL_ACCESS_TOKEN\')\nLINE_CHANNEL_SECRET = os.environ.get(\'LINE_CHANNEL_SECRET\')\nline_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)\nhandler …
Run Code Online (Sandbox Code Playgroud)

python google-api google-api-python-client google-sheets-api

6
推荐指数
1
解决办法
8055
查看次数

PyDrive guath.Refresh() 和刷新令牌问题

像其他人一样,我试图让 Google 刷新令牌工作,以便运行复制和重命名文件的计划任务。

当我第一次在终端内手动验证时,我的 url 以 &access_type=offline 结尾。但是,当我进入并尝试在 ipython 中手动使用 gauth.Refresh() 时,它失败并出现与我的凭据文件过期时相同的错误:

pydrive.auth.RefreshError: No refresh_token found.Please set access_type of OAuth to offline.
Run Code Online (Sandbox Code Playgroud)

我如何实际将 access_type 设置为离线?任何建议都非常感谢。

我一直在这里、这里和这里试图解决这个问题。

我的脚本:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()

# Try to load saved client credentials
gauth.LoadCredentialsFile("GoogleDriveCredentials.txt")
if gauth.credentials is None:
    # Authenticate if they're not there
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    # Refresh them if expired
    print "Google Drive Token Expired, Refreshing"
    gauth.Refresh()
else:
    # Initialize …
Run Code Online (Sandbox Code Playgroud)

oauth google-api google-api-python-client pydrive

5
推荐指数
1
解决办法
3304
查看次数

使用 Google API 发送邮件时出错 - “'raw' RFC822 有效负载消息字符串或通过 /upload/* URL 上传消息需要”

我正在尝试通过 Python 中的 Google API 发送消息,并尝试运行几乎直接从Google 示例页面获取的示例。

def CreateMessage(sender, to, subject, message_text):
    message = MIMEText(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    return {'raw': base64.urlsafe_b64encode(message.as_string().replace('message','resource').encode('ascii'))}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试发送它时

    message = CreateMessage(sender, to, subject, message_text)
    message = service.users().messages().send(body=list(message),userId='me').execute()
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:“'raw' RFC822 有效负载消息字符串或通过 /upload/* URL 上传消息需要”

从其他帖子看来,谷歌似乎在期待一个附件。MIMEText 有什么问题让它期待一个,如果有,我该如何解决?

python-3.x google-api-python-client gmail-api

5
推荐指数
2
解决办法
2055
查看次数

使用 oauth2 和 Google API 无法识别的参数

我在某些脚本中使用了 Google API 服务,但遇到了一些问题。这个错误很奇怪,但我们走了。我有一个列出我的 Google Drive 文件的脚本。

from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'
store = file.Storage('storage.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)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))

files = DRIVE.files().list().execute().get('files', [])
for f in files:
    print(f['name'], f['mimeType'],f['id'])
Run Code Online (Sandbox Code Playgroud)

它运行良好,我client_secret.json从 Google API下载并将其保存在同一文件夹中,然后我启动脚本以检查一切正常。

然后我开始编辑我的文件以改变我执行它的方式而不是读取文件,而是调用脚本并将client_id和client_secret字符串发送到脚本,最终版本是这样的:

import sys
from apiclient import discovery
from httplib2 import Http
from oauth2client …
Run Code Online (Sandbox Code Playgroud)

python google-api python-3.x oauth2client google-api-python-client

5
推荐指数
1
解决办法
909
查看次数

How to overwrite a file using Google Drive API with Python?

I want to create a simple script which will upload a file to my Drive every 5 minutes using cronjob. This is the code I have so far using the boilerplate code I extracted from different locations (mainly 2: getting started page & create page):

from __future__ import print_function
from apiclient import errors
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaFileUpload

def activateService():
    creds = None
    if os.path.exists('token.pickle'): …
Run Code Online (Sandbox Code Playgroud)

python google-api google-drive-api google-api-python-client

5
推荐指数
1
解决办法
3252
查看次数