如何通过API密钥登录Google Drive

问题描述 投票:0回答:2

我按照文档中的描述进行操作:

#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 返回“需要登录”>

我做错了什么?

python google-drive-api
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)

0
投票

由于

children
似乎仅限于OAuth2.0,这里是我为自己编写的与
API_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>')
© www.soinside.com 2019 - 2024. All rights reserved.