2013-01-11 149 views
1

documentation描述我登录到谷歌驱动器:如何通过API密钥

#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() 

错误:

An error occurred: https://www.googleapis.com/drive/v2/files/yyyyyyy/children?alt=json&key=xxxxx returned "Login Required">

我在做什么错?

回答

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>')