Ich löste dieses Problem, indem ich einen API-Schlüssel für das Projekt, an dem ich arbeite, in der Cloud-Konsole erstellte.
Ich erstellte eine Datei http_response.json und kopierte ein Beispiel der Antwort, die ich erwarte. ich verwenden, um den folgenden Ausschnitt:
import unittest
from apiclient.discovery import build
from apiclient.http import HttpMock
import original_file
from apiclient.discovery import build
class TestAdmin(unittest.TestCase):
def setUp(self):
server_response = 'Blah Blah Blah' # sample server response
with open('http_response.json','w') as response_file:
json.dump(server_response, response_file)
def test_admin(self):
http = HttpMock('http_response.json', {'status': '200'})
api_key = 'api key created on google cloud'
service_object = build('admin', 'directory_v1', http=http, developerKey=api_key)
# The call to build should be made to a method
# in the original class that implements the build service
request = service_object.users().list(customer='my_customer', maxResults=1, orderBy='email',query='/')
http = HttpMock('allusers.json', {'status': '200'})
response = request.execute(http=http)
self.assertEquals(response, server_response)
def tearDown(self):
# delete the temporary file created
try:
os.remove(self.user_response)
except OSError:
pass
if __name__ == '__main__':
unittest.main()
ich dies aus getippt. Es kann Tippfehler geben :)