【问题标题】:Google API and Django getting 400 error when trying to copy a folder尝试复制文件夹时,Google API 和 Django 出现 400 错误
【发布时间】:2018-01-02 02:08:08
【问题描述】:

我正在尝试使用 google api、python 和 Django 复制一个文件夹,并设法通过修改 here 找到的代码来完成工作

但是当我尝试复制一个文件夹时,我得到:

https://www.googleapis.com/drive/v3/files/FileId/copy?alt=json时的HttpError 400返回“Bad Request”>

import os
import logging
import httplib2
from pprint import pprint
from googleapiclient.discovery import build
from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.http import HttpResponseBadRequest
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.http import JsonResponse
from django.db.utils import IntegrityError
from .models import Entries, CredentialsModel
from oauth2client.contrib import xsrfutil
from oauth2client.client import flow_from_clientsecrets
from oauth2client.contrib.django_util.storage import DjangoORMStorage
from .pipeline_lib import pipeline_lib as pipeline

FLOW = flow_from_clientsecrets(
    settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
    scope=['https://www.googleapis.com/auth/drive',
           'https://www.googleapis.com/auth/drive.file',
           'https://www.googleapis.com/auth/drive.appdata',
           'https://www.googleapis.com/auth/drive.metadata'],
    redirect_uri='http://somedomain.com:8000/workflow/oauth2callback')

@login_required
def index(request):
    storage = DjangoORMStorage(CredentialsModel, 'id', request.user, 'credential')
    credential = storage.get()
    if credential is None or credential.invalid == True:
        FLOW.params['state'] = xsrfutil.generate_token(settings.SECRET_KEY,
                                                       request.user)
        authorize_url = FLOW.step1_get_authorize_url()
        return HttpResponseRedirect(authorize_url)
    else:
        http = httplib2.Http()
        http = credential.authorize(http)
        service = build("drive", "v3", http=http)
        newfile = {'title': 'New Master 123', 'parents': [{'id': 'folderId'}]}
        result = service.files().copy(fileId='folderId',body=newfile).execute()
        pprint(result)
        # results = service.files().list(pageSize=10,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('{0} ({1})'.format(item['name'], item['id']))
        return HttpResponse("Hello, world. You're at the index.")

@login_required
def auth_return(request):
    credential = FLOW.step2_exchange(request.GET)
    storage = DjangoORMStorage(CredentialsModel, 'id', request.user, 'credential')
    storage.put(credential)
    return HttpResponseRedirect("/workflow")

【问题讨论】:

    标签: python django google-api google-drive-api drive


    【解决方案1】:

    问题是我试图复制一个文件夹,并且显然 files().copy 不能在一个文件夹上操作,至少不是一个有孩子的文件夹,但我还没有测试这个警告。

    在将我要复制的文件夹的 ID 替换为同一父级中的 pdf 的文件 ID 后,该函数运行时没有出现错误。

    编辑 - 现在我已经自己弄清楚了,我偶然发现了一个堆栈溢出帖子,它解释了为什么会这样。 In Google Drive SDK how do you copy a folder?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 2023-04-03
      • 1970-01-01
      • 2020-11-21
      • 2019-09-18
      相关资源
      最近更新 更多