2016-01-27 62 views
2

我刚开始尝试使用Google Drive API。使用快速入门指南设置身份验证,我可以打印我的文件列表,甚至可以制作副本。所有这些都很好,但是我试图访问Drive上文件的数据时遇到了问题。特别是,我试图获得WebViewLink,但是当我打电话给.get时,我只收到一个几乎没有任何文件元数据的小字典。 The documentation使得它看起来像所有的数据应该默认在那里,但它没有出现。我无法找到任何标示请求任何附加信息的方式。使用Google Drive获取WebViewLinks

credentials = get_credentials() 
http = credentials.authorize(httplib2.Http()) 
service = discovery.build('drive', 'v3', http=http) 

results = service.files().list(fields="nextPageToken, files(id, name)").execute() 
items = results.get('files', []) 
if not items: 
    print('No files found.') 
else: 
    print('Files:') 
    for item in items: 
     print(item['name'], item['id']) 
     if "Target File" in item['name']: 
      d = service.files().get(fileId=item['id']).execute() 
      print(repr(d)) 

这是上面代码的输出:(格式是我做的)

{u'mimeType': u'application/vnd.google-apps.document', 
u'kind': u'drive#file', 
u'id': u'1VO9cC8mGM67onVYx3_2f-SYzLJPR4_LteQzILdWJgDE', 
u'name': u'Fix TVP Licence Issues'} 

对于任何人困惑的代码有一些缺失,这只是从API的quickstart page基本get_credentials功能和一些常数和进口。为了完整起见,这里的所有的东西,未修改在我的代码:

from __future__ import print_function 
import httplib2 
import os 

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

SCOPES = 'https://www.googleapis.com/auth/drive' 
CLIENT_SECRET_FILE = 'client_secret.json' 
APPLICATION_NAME = 'Drive API Python Quickstart' 

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


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, 
            'drive-python-quickstart.json') 

    store = oauth2client.file.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 

所以缺少什么,我怎么能得到API返回所有这只是不是现在出现额外的元数据?

回答

3

你非常接近。使用较新版本的Drive API v3,要检索其他元数据属性,您必须添加fields参数来指定要包含在部分响应中的其他属性。

在你的情况,因为你正在寻找检索WebViewLink财产您的要求应该与此类似:

results = service.files().list(
     pageSize=10,fields="nextPageToken, files(id, name, webViewLink)").execute() 

要从回应显示您的物品:

for item in items: 
      print('{0} {1} {2}'.format(item['name'], item['id'], item['webViewLink'])) 

我也建议使用API Explorer进行试用,以便查看您希望在响应中显示哪些其他元数据属性。

祝你好运,希望这会有所帮助! :)

+0

啊哈,确切的问题。这是我没有阅读我密切复制的代码。谢谢! – SuperBiasedMan

+0

不客气。很高兴我能够帮助! :) – Andres

1

您明确要求您的files.list调用中的idname字段。将webViewLink添加到列表中results = service.files().list(fields="nextPageToken, files(id, name, webViewLink)").execute()。要检索所有元数据files/*应该被使用。有关此性能优化的更多信息,请参阅Google云端硬盘文档中的Working with partial resources

+0

但是,方法“得到”还返回所有的元数据?在我的情况下,这些属性是空的,不知道为什么,即使有正确的权限! – Miguel

+0

[Files:get](https://developers.google.com/drive/v3/reference/files/get)中的API资源管理器返回字段request参数中指定的元数据,因此您可能遇到不同的问题,例如,在我的应用程序中,当API浏览器需要更多时,'auth/drive'作用域似乎足够了。 –

+0

那么这些字段是空的,因为我没有使用“fields”参数来获取它们,并且需要指定要提取哪些数据。 – Miguel

相关问题