3

我按照文档中的说明进行操作:

#google key
API_key = "xxxxx"
#creating an instance of the class
drive_service = build('drive', 'v2', developerKey = API_key)
#get a list of child folder in
children = drive_service.children().list(folderId='yyyyyyy', **param).execute()

一个错误:

发生错误:https://www.googleapis.com/drive/v2/files/yyyyyyy/children?alt=json&key=xxxxx 返回“需要登录”>

我究竟做错了什么?

4

2 回答 2

3

这是一个极简版本,演示了使用 GDrive API V3 进行文件夹查询。

import httplib2
import pprint
import sys
from apiclient.discovery import build

def printChildren(parent):
  param = {"q": "'" + parent + "' in parents and mimeType != 'application/vnd.google-apps.folder'"}
  result = service.files().list(**param).execute()
  files = result.get('files')

  for afile in files:
    print('File {}'.format(afile.get('name')))

API_KEY = 'XXXXXXX' # get from API->Credentials page in console.cloud.googl.com
FOLDER_ID = '1bWJ18tyq1h-ABQT79SgTsJQFRCIIblHS' # NOTE: folder must be publicly visible when using an API key.
service = build('drive', 'v3', developerKey=API_KEY)
printChildren(FOLDER_ID)
于 2020-11-28T19:04:30.330 回答
0

由于children似乎仅限于OAuth2.0API_KEY ,因此这是一个我为自己编写的示例程序供使用。使用它,您可以通过文件夹进行递归并执行您将使用的大部分内容children

import httplib2
import pprint
import sys
from apiclient.discovery import build

# The API key of the project.
API_KEY = '<yourapikey>'

def createDriveService():
    """Builds and returns a Drive service object authorized with the
    application's service account.

    Returns:
        Drive service object.
    """

return build('drive', 'v2', developerKey=API_KEY)

service = createDriveService()
def recurse(parent):
    def recurseFolders():
        result = []
        page_token = None
        while True:
            param = { "q": "'" + parent + "' in parents and mimeType =  'application/vnd.google-apps.folder'" }
            if page_token:
                param['pageToken'] = page_token
            files = service.files().list(**param).execute()
            result.extend(files['items'])
            page_token = files.get('nextPageToken')
            if not page_token:
                break

        for folder in result:
            recurse(folder.get("id"))

    def printChildren():
        result = []
        page_token = None
        while True:
            param = { "q": "'" + parent + "' in parents and mimeType != 'application/vnd.google-apps.folder'" }
            if page_token:
                param['pageToken'] = page_token
            files = service.files().list(**param).execute()
            result.extend(files['items'])
            page_token = files.get('nextPageToken')
            if not page_token:
                break

        for afile in result:
            # Cannot use webViewLink, because it's only valid for static html content
            print afile.get('title') + u':' + u'"' + afile.get("webContentLink") + u',"'

    recurseFolders();
    printChildren();
recurse('<folder_id>')
于 2013-04-05T09:16:22.687 回答