Reputation: 1252
I am trying to copy a folder using the google api, python and Django and have managed to get things working with an adaptation of the code found here
But when I try to copy a folder I get:
< HttpError 400 when requesting https://www.googleapis.com/drive/v3/files/FileId/copy?alt=json returned "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")
Upvotes: 0
Views: 219
Reputation: 1252
The problem was that I was attempting to copy a folder and apparently files().copy doesn't operate on a folder at least not one with children but I haven't tested the caveat yet.
After replacing the id of the folder I wanted to copy with the file id of a pdf in the same parent the function ran with out error.
Edit - Now that I've figured this out for myself I stumbled upon a stack overflow post that explains why this is. In Google Drive SDK how do you copy a folder?
Upvotes: 1