2

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?

2 Answers 2

2

SOLVED

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:

  • I found my API_KEY in Google API Console.
  • HTTP request methond is 'POST'
  • The token value start with "Bearer" word, don't "OAuth" (deprecated).
  • Parameter requiered isn't a request header. It's the data of the request.
  • The 'data' is json format but binary.
  • header "Content-Type: application/json" is requiered
  • header "Content-Length: " is requiered

've uploaded a Gist with the completed example fine working: https://gist.github.com/SalvaJ/9722045

Sign up to request clarification or add additional context in comments.

Comments

0

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}"

1 Comment

I already added the value of key= but the response is a 404 error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.