2016-01-27 114 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

相關問題