标签: google-api-python-client

Drive API请求中未列出任何Google云端硬盘文件

我正在尝试使用桌面应用获取Google云端硬盘中的文件列表.代码如下:

def main(argv):

    storage = Storage('drive.dat')
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        credentials = run(FLOW, storage)

    # Create an httplib2.Http object to handle our HTTP requests and authorize it
    # with our good Credentials.
    http = httplib2.Http()
    http = credentials.authorize(http)

    service = build("drive", "v2", http=http)
    retrieve_all_files(service)
Run Code Online (Sandbox Code Playgroud)

然后在retrieve_all_files中,我打印文件:

param = {}
if page_token:
    param['pageToken'] = page_token
    files = service.files().list(**param).execute()
    print files
Run Code Online (Sandbox Code Playgroud)

但在我对我的帐户进行身份验证后,打印的文件列表中没有任何项目.有没有人有类似的问题或知道解决方案?

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

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

如何在GAE cron作业中执行OAuth操作?

这篇文章是如何在GAE任务队列中执行OAuth要求操作的后续内容?.正如bossylobster在这篇文章中所建议的,我确实尝试user_id在我的脚本中进行硬编码,但是cron任务总是失败:

  • 我可以看到它在日志中被触发......

    2013-05-31 14:20:00.879 /update 302 5ms 0kb AppEngine-Google; (+http://code.google.com/appengine)
    0.1.0.1 - - [31/May/2013:11:20:00 -0700] "GET /update HTTP/1.1" 302 385 - "AppEngine-Google; (+http://code.google.com/appengine)" "myapp.appspot.com" ms=6 cpu_ms=0 cpm_usd=0.000043 queue_name=__cron task_name=... app_engine_release=1.8.0 instance=...
    
    Run Code Online (Sandbox Code Playgroud)
  • ...但是,它总是失败(在AppEngine的Cron Jobs部分显示为红色"失败"),它应该运行的任务队列永远不会弹出任务队列部分.

我不明白的是,如果我/update手动打开,我有下面的登录提示.cron如何绕过那个?我该如何调试正在进行的操作?

GAE登录提示

谢谢您的帮助.

在@dlebech答案编辑/后随:问题添加后仍继续login: admin行有关url在app.yaml.在本地(通过以管理员身份登录并访问您的浏览器中的处理程序的URL,如安全URL for Cron所述)一切顺利,但一旦部署,我的应用程序永远不会超过OAuth2装饰器:

  • 有几个调试print语句......

    import webapp2
    ...
    
    YOUTUBE_RW_SCOPE = "https://www.googleapis.com/auth/youtube"
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = …
    Run Code Online (Sandbox Code Playgroud)

python cron google-app-engine google-api google-api-python-client

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

bq.py不分页结果

我们正在为bq.py编写一个包装器,并且在结果集大于100k行时遇到了一些问题.似乎在过去这个工作正常(我们在Google BigQuery不完整查询回复奇怪尝试时遇到了相关问题).也许我不理解doc页面上解释的限制?

例如:

#!/bin/bash

for i in `seq 99999 100002`;
do
    bq query -q --nouse_cache --max_rows 99999999 "SELECT id, FROM [publicdata:samples.wikipedia] LIMIT $i" > $i.txt
    j=$(cat $i.txt | wc -l)
    echo "Limit $i Returned $j Rows"
done
Run Code Online (Sandbox Code Playgroud)

收益率(注意有4行格式化):

Limit 99999 Returned   100003 Rows
Limit 100000 Returned   100004 Rows
Limit 100001 Returned   100004 Rows
Limit 100002 Returned   100004 Rows
Run Code Online (Sandbox Code Playgroud)

在我们的包装器中,我们直接访问API:

while row_count < total_rows:
    data = client.apiclient.tabledata().list(maxResults=total_rows - row_count,
                                                 pageToken=page_token,
                                                 **table_dict).execute()

    # If there are …
Run Code Online (Sandbox Code Playgroud)

python google-bigquery google-api-python-client

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

由PyInstaller打包到exe后,Errno 185090050 _ssl.c:343:错误:0B084002:x509证书例程:X509_load_cert_crl_file:system lib

我编写了一个Python脚本来检查GCS中的文件,它使用wxpython生成GUI。为了进行身份验证,我是以这种方式进行的(遵循Google示例代码中的方法-> http://code.google.com/p/google-cloud-platform-samples/source/browse/file-transfer-json/chunked_transfer。 py?repo = storage):

flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, 
                                # the secrets file I put in same folder
                               scope=scope,
                               message=MISSING_CLIENT_SECRETS_MESSAGE)

credential_storage = CredentialStorage(CREDENTIALS_FILE) # the file to store
                                                    # authentication credentials
credentials = credential_storage.get()
if credentials is None or credentials.invalid:
    credentials = run_oauth2(flow, credential_storage)

self.printLog('Constructing Google Cloud Storage service...')
http = credentials.authorize(httplib2.Http())
return discovery_build('storage', 'v1beta1', http=http)
Run Code Online (Sandbox Code Playgroud)

上面的代码包含在我的Python脚本中,当它只是一个python .py文件时效果很好,后来我用pyinstaller在win 7 64bit中将其转换为.exe(我也将秘密文件与.exe文件),使用以下命令

C:\ gcs_file_check> python pyinstaller-2.0 \ pyinstaller.py -w gcs_file_check.py

单击exe时,正确启动了Google 的“ 请求权限”页面(因为它运行的是Python脚本而不是exe),但是单击“接受”后,上面的代码将引发异常:

[Errno 185090050] _ssl.c:343:错误:0B084002:x509证书例程:X509_load_cert_crl_file:系统库

而且我可以看到文件certificate.json不是由.exe创建的,而Python脚本可以正确创建此文件。

有人知道这是怎么发生的以及如何解决吗?感谢每个答案!

=================== …

python pyinstaller google-cloud-storage google-api-python-client

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

AccessOokenRefreshError:来自oauth2client的invalid_grant

我正在创建一个脚本,使用Google-api-python-client为特定的Google Apps用户下载文件.首先,我试图获取用户帐户包含的文件列表.我按照链接https://developers.google.com/drive/v2/reference/files/list中给出的示例进行操作

代码块如下

用户对客户的授权

def authorize_application(request):

    #setting flow to get permission and code 
    flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI, ACCESS_TYPE)
    authorize_url = flow.step1_get_authorize_url()

    code = request.GET.get('code', '')

    if code:
        #import ipdb
        #ipdb.set_trace()
    #setting flow step2 to exchage code for access token    
        credential = flow.step2_exchange(code)

    #initialising httplib2 instance and building a DriveAPI service
        http = httplib2.Http()
        http = credential.authorize(http)
        drive_service = build('drive', 'v2', http=http)

        # getting user's data
    about = drive_service.about().get().execute()
        user_info = about.get('user')
        email = user_info.get('emailAddress')
        request.session['username'] = …
Run Code Online (Sandbox Code Playgroud)

python google-api-python-client

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

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 OAuth按范围撤销访问令牌

我们正在处理Google身份验证范围,并且在撤销方面遇到了问题。

1-我们已授权用户使用他的Google Analytics(分析)帐户

2-然后,他可以添加自己的GMAIL帐户可以切换以上两个步骤

3-现在,如果我们想撤消访问权限,则两个授权都被撤消

我们已经多次浏览了Google文档,找不到根据范围撤消令牌的位置

我们要做的是允许用户选择要撤消的Google App令牌

  • 用户选择删除Google Analytics(分析)令牌或GMAIL令牌

有没有人遇到过这样的事情并且找到了一种做这种事情的方法?

gmail google-analytics oauth oauth-2.0 google-api-python-client

5
推荐指数
0
解决办法
732
查看次数

Google表格API"更新"方法Http Error 400

我正在尝试创建一个读取和写入谷歌电子表格的python脚本.我基本上已经在https://developers.google.com/sheets/quickstart/python上复制了python快速入门脚本,并使用https://developers.google.com/resources/api-libraries/documentation/上的参考进行了修改.sheets/v4/python/latest /.

使用快速入门脚本中显示的"get"方法,一切正常.我可以看到没有错误的表格.要使用"更新"而不是"获取"(写入工作表而不是读取它),我删除.readonly了范围网址的部分.我还get()使用update()包含编码的值的json对象替换方法并将其body作为参数包含在update()方法中json.dumps.这完全根据上面的第二个参考.

每次"无效数据"都会出现HttpError 400.

码:

import httplib2
import os
import json

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Sheets API Python …
Run Code Online (Sandbox Code Playgroud)

python google-spreadsheet-api google-api-python-client

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

使用 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
查看次数

几次请求后超出了用户速率限制

我通过pydrive使用Google Drive API 在两个谷歌驱动器帐户之间移动文件.我一直在测试一个包含16个文件的文件夹.我的代码总是在第六个文件中引发错误

"超出用户速率限制">

我知道请求数量有限制(10/s或1000/100s),但我已尝试使用Google Drive API建议的指数退避来处理此错误.即使在248秒之后,它仍然会引发同样的错误.

这是我正在做的一个例子

def MoveToFolder(self,files,folder_id,drive):
    total_files = len(files)
    for cont in range(total_files):
        success = False
        n=0
        while not success:
            try:
                drive.auth.service.files().copy(fileId=files[cont]['id'],
                                                body={"parents": [{"kind": "drive#fileLink", "id": folder_id}]}).execute()
                time.sleep(random.randint(0,1000)/1000)
                success = True
            except:
                wait = (2**n) + (random.randint(0,1000)/1000)
                time.sleep(wait)
                success = False
                n += 1
Run Code Online (Sandbox Code Playgroud)

我尝试使用"批处理请求"来复制文件,但它会为10个文件引发相同的错误.

def MoveToFolderBatch(self,files,folder_id,drive):
    cont=0
    batch = drive.auth.service.new_batch_http_request()
    for file in files:
        cont+=1
        batch.add(drive.auth.service.files().copy(fileId=file['id'],
                                                 body={"parents": [
                                                     {"kind": "drive#fileLink", "id": folder_id}]}))
    batch.execute()
Run Code Online (Sandbox Code Playgroud)

有人有任何提示吗?

编辑:根据谷歌支持:

关于超出用户速率限制错误,与控制台中设置的每用户速率限制完全无关.相反,它来自Drive API依赖的内部Google系统,并且最有可能在单个帐户拥有域中的所有文件时发生.我们不建议单个帐户拥有所有文件,而是让域中的个人用户拥有这些文件.要传输文件,您可以查看此链接.另外,请检查建议上的此链接以避免错误.

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

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