私は、クラウドコンソールで作業しているプロジェクトのAPIキーを作成することでこの問題を解決しました。
ファイルhttp_response.jsonを作成し、私が期待している応答のサンプルをコピーしました。
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()
私はこれを入力した: 私は、次のスニペットを使用しました。タイプミスがある可能性があります。