Reputation: 2463
I want to make an installed aplication in Python3 to manage the Google Drive files in my google account.
As the official google-api-python-client doesn't support Python3 I've decided to code myself the oauth2 method and access to google drive api with urlib.request.
I managed to pass the authentication process and get the token. Then I tried to access to the google drive api (copying a file) following the api reference: POST https://www.googleapis.com/drive/v2/files/fileId/copy
with this code:
def copy_file(token, target_name):
print("Access Token: " + token)
url_target = "https://www.googleapis.com/drive/v2/files/0Akg4S5DP95FAdFM3VXNJbVo4TjM0MFFGVm5hWlFtU2c/copy"
request = urllib.request.Request(url_target)
request.add_header("Authorization", "OAuth" + token)
request.add_header("title", target_name)
f = urllib.request.urlopen(request)
print(f.read())
I only get 404 error.
When I try with the Google Api Explorer it work ok:
Request
POST https://www.googleapis.com/drive/v2/files/0Akg4S5DP95FAdFM3VXNJbVo4TjM0MFFGVm5hWlFtU2c/copy?key={YOUR_API_KEY}
Content-Type: application/json
Authorization: Bearer ya29.1.AADtN_ULTFZ3jvv962bVVjAYv_GknktRMgvIGAGJPdZ5OAocQANLmN5q_UMq5cA53aqoHBkqo39wHiGM1-pg
X-JavaScript-User-Agent: Google APIs Explorer
{
"title": "copia de HiperAgenda"
}
Response
200 OK
I've omitted in my code this ?key={YOUR_API_KEY}
Where is my Api Key?
What's my wrong?
Upvotes: 2
Views: 1875
Reputation: 2463
def copyFile(token, target_name):
print("Access Token: " + token)
url_destino = "https://www.googleapis.com/drive/v2/
files/0AilPd9i9ydNTdFc4a2lvYmZnNkNzSU1kdVFZb0syN1E/copy
?key=(YOUR_API_KEY provided by Google API Console)"
values = "{'title': '%'}" % target_name
data = values.encode('utf-8')
request = urllib.request.Request(url_destino, data, method='POST')
request.add_header("Authorization", "Bearer " + token)
request.add_header("Content-Length", len(data))
request.add_header("Content-Type", "application/json")
print(request.header_items())
f = urllib.request.urlopen(request)
print(f.read())
Fixed errors:
've uploaded a Gist with the completed example fine working: https://gist.github.com/SalvaJ/9722045
Upvotes: 2
Reputation: 6254
You're not adding the value of the copy
request argument in your Python implementation (i.e. the part in the API explorer that says "copy?key={YOUR_API_KEY}
"
Upvotes: 0