我正在尝试使用“ get”获取文件的URL链接。 Web版本的API返回所有属性或“键”。但是,当我尝试通过Python代码访问它时,它仅返回有限数量的键,如“ mimetype”之类的项目,但没有URL信息。我认为已包含适当的“范围”,并且授权文件“ token.json”是当前的,但是当对目标文件进行查询时,代码将引发“ keyError”。下面是运行代码时的响应。它找到了文件,但是仅报告了有限数量的密钥,如下所示:
{u'mimeType': u'image/gif', u'kind': u'drive#file', u'id': u'0B3rj986AqarFMHBWNng1T043WkE', u'name': u'sketchImage.gif'}
Traceback (most recent call last):
File "C:/Python27/stackQuestion.py", line 35, in <module>
main()
File "C:/Python27/stackQuestion.py", line 33, in main
print(myFile['alternateLink'])
KeyError: 'alternateLink'
代码
from __future__ import print_function
import datetime
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata',
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive.photos.readonly',
'https://www.googleapis.com/auth/drive.readonly'
]
def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
# Build Drive Service Instances
serviceF = build('drive', 'v3', http=creds.authorize(Http()))
#Locate the file by "fileID"
myFile= serviceF.files().get(fileId=u'0B3rj986AqarFMHBWNng1T043WkE').execute()
print(myFile)
print(myFile['alternateLink']) # <===== My Difficulty
if __name__ == '__main__':
main()