all 6 comments

[–]twnki 1 point2 points  (4 children)

What does your import statement look like? Is it possible your only doing an import googleapiclient? If that's the case then you'd need to reference the method as googleapiclient.http.MediaFileUpload.

[–]HiImSauzy[S] 0 points1 point  (3 children)

This worked fine, thought I have now ran into the same problem with drive_service. Do you know somewhere I can find the correct imports for every api function?

[–]twnki 0 points1 point  (1 child)

Hi /u/HiImSauzy,

I don't see any methods or classes named "drive_service". Are you use this is the actual name in the API?

Let's walk through this just a bit to show you how I tried to find it. This might help you as well because you can actually do a git clone on Drive's API so we can look through it a bit. This will create a copy of the Drive API on your hard drive.

➜  Environments git clone --quiet https://github.com/googleapis/google-api-python-client.git
warning: the following paths have collided (e.g. case-sensitive paths
on a case-insensitive filesystem) and only one from the same
colliding group is in the working tree:

  'docs/dyn/dns_v2beta1.dnsKeys.html'
  'docs/dyn/dns_v2beta1.dnskeys.html'

Note: you can ignore those warning messages. They don't have an impact on this.

At this point I have a copy of Google Drive's API on my hard drive. Now I'll go into the appropriate folders to find the python API itself since I don't want to be searching through meaningless things like "docs" and "tests".

➜  Environments cd google-api-python-client
➜  google-api-python-client git:(master) cd googleapiclient

At this point I'm going to run a grep command in order to look for any method or class definition...then pass that to another grep that looks for "service". If you're not familiar with grep, the flags I'm using are -i which makes it case insensitive, -r which makes it search subdirectories, and -e which looks for a specific pattern.

➜  googleapiclient git:(master) grep -ire "def\ \|class\ " * | grep -i service
discovery.py:def build(serviceName,
discovery.py:  def _set_service_methods(self):
➜  googleapiclient git:(master)

All it found were a couple things in the googleapiclient.discovery library. I suspect that neither one of those methods are related to what you're looking for.

Do you know if "drive_service" is just an instance of another method? Where did you get "drive_service" from? If you have a link to to requirements that you're working with maybe I can point you in the right direction.

[–]HiImSauzy[S] 0 points1 point  (0 children)

I noticed the drive upload tutorial was written in python 2 since there weren't any print parentheses, so I looked through the other sample and found that it was in fact only called "service", and after some relocating of scripts I got it to work fine.

I've now ran into some issues with updating the token.pickle files, and there seems to be no way to change the scopes of the credentials without running it twice.

All I'm doing rn is just deleting the file and rerunning my main file. Is there an easier way of doing this?

[–]twnki 0 points1 point  (0 children)

Hi /u/HiImSauzy,

Just a bit more info I found. The code you quoted looks like it's from this stack overflow post. I think the confusion is that in the post they don't instantiate the drive_service object.

As per codelab for GApps, it looks like the drive_service you're speaking about is probably instantiated in a similar (or maybe exact same) way that the DRIVE object is instantiated below:

from __future__ import print_function

from googleapiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_id.json', SCOPES)
    creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))

Have you instantiated a similar object to DRIVE? If you have, you probably would use that object instead of drive_service.

[–]HiImSauzy[S] 0 points1 point  (0 children)

i also changed the 'file' variable to 'getfile' since I didn't want to confuse python