Google Drive API file copy operation failing with 400 error

I’m stuck trying to copy a file to a folder using the Google Drive API. I’m sending a POST request to the copy endpoint, but it’s not working as expected.

Here’s what I’m doing:

import urllib2
import urllib

url = 'https://www.googleapis.com/drive/v2/files/FILE_ID_HERE/copy?access_token=YOUR_ACCESS_TOKEN'
payload = {
    'parents': [
        {
            'id': 'TARGET_FOLDER_ID'
        }
    ]
}

req = urllib2.Request(url, urllib.urlencode(payload))
response = urllib2.urlopen(req)
result = response.read()
response.close()
print(result)

But I keep getting a 400 Bad Request error. What’s weird is that when I try the same thing in the Google API Playground, it works fine. Any ideas what I might be doing wrong? Maybe I’m missing something in the request setup?

I’ve encountered similar issues with the Google Drive API before. The problem might be in how you’re structuring your payload. For POST requests, you typically need to send JSON data, not URL-encoded form data.

Try modifying your code like this:

import json
import urllib2

url = 'https://www.googleapis.com/drive/v2/files/FILE_ID_HERE/copy?access_token=YOUR_ACCESS_TOKEN'
payload = json.dumps({
    'parents': [{'id': 'TARGET_FOLDER_ID'}]
})

req = urllib2.Request(url, payload)
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req)
result = response.read()
response.close()
print(result)

This change should resolve the 400 error. Remember to replace FILE_ID_HERE, YOUR_ACCESS_TOKEN, and TARGET_FOLDER_ID with your actual values. If you’re still having trouble, double-check that your access token is valid and has the necessary permissions.

hey mate, had similar issues. try using the requests library instead of urllib2. it’s way easier to work with. something like:

import requests
import json

url = 'https://www.googleapis.com/drive/v2/files/FILE_ID/copy'
headers = {'Authorization': 'Bearer YOUR_TOKEN', 'Content-Type': 'application/json'}
data = json.dumps({'parents': [{'id': 'FOLDER_ID'}]})

response = requests.post(url, headers=headers, data=data)
print(response.json())

hope this helps!

I’ve been working with the Google Drive API for a while now, and I can tell you that the 400 error often comes down to how you’re formatting the request. In my experience, using the google-auth and google-auth-oauthlib libraries along with the official google-api-python-client can save you a lot of headaches.

Here’s a snippet that’s worked reliably for me:

from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials

creds = Credentials.from_authorized_user_file('path/to/token.json')
service = build('drive', 'v3', credentials=creds)

file_metadata = {
    'name': 'Copied file',
    'parents': ['TARGET_FOLDER_ID']
}

copied_file = service.files().copy(
    fileId='SOURCE_FILE_ID',
    body=file_metadata
).execute()

print(f\"File copied: {copied_file.get('id')}\")

This approach handles authentication and request formatting for you, which often resolves these pesky 400 errors. Just make sure your token file is up-to-date and has the necessary scopes. If you’re still hitting issues, double-check your file and folder IDs - that’s tripped me up more times than I’d like to admit!