0

我试图使用Google Api Discovery HttpMockSequence,但我不断收到以下错误:如何嘲笑谷歌API探索HttpResponseSequence

InvalidJsonError: 
-------------------- >> begin captured logging << -------------------- 
googleapiclient.discovery: INFO: URL being requested: GET https://www.googleapis.com/discovery/v1/apis/webmasters/v3/rest 
googleapiclient.discovery: ERROR: Failed to parse as JSON: /home/user/development/projectname/tests/build_response_data.json 
--------------------- >> end captured logging << --------------------- 

这里是我/试图做:

tests.py

build_response = self.datafile('build_response_data.json') 
request_data = self.datafile('saved_request_data.json') 

http_auth = HttpMockSequence([ 
    ({'status': '200'}, build_response), 
    ({'status': '200'}, request_data) 
]) 

service = build('webmasters', 
       'v3', 
       http=http_auth, 
       developerKey='myapikey1234') 

,你可以在这里看到我的JSON文件: build_response_data.json

问题发生在文件中googleapiclient/discovery.py

我只是不能似乎明白了这是怎么回事on..can谁能告诉我就行了功能_retrieve_discovery_doc 253我究竟做错了什么?

回答

2

要anwser我自己的问题,并帮助其他人在同一条船上,问题是,_retrieve_discovery_doc试图将funtion以内容为JSON加载按该行253:

service = json.loads(content) 

,但因为我是不打开文件并阅读它的内容,json实际上是试图加载url为json。

所以对于第一部分的解决办法是改变HttpMockSequence到:

http_auth = HttpMockSequence([ 
    ({'status': '200'}, open(build_response, 'rb').read()), 
    ({'status': '200'}, open(request_data, 'rb').read()) 
]) 

然后从request_data文件你做返回的数据:

site_url = 'www.example.com' 
body = { 
    'startDate': '2015-09-11', 
    'endDate': '2015-12-12', 
    'dimensions': ['date'] 
} 
build_response_data = self.datafile('build_response_data.json') 
request_data = self.datafile('saved_request_data.json') 
http_auth = HttpMockSequence([ 
    ({'status': '200'}, open(build_response_data, 'rb').read()), 
    ({'status': '200'}, open(request_data, 'rb').read()) 
]) 

service = build('webmasters', 
       'v3', 
       http=http_auth, 
       developerKey='myapikey1234') 
service.searchanalytics().query(siteUrl=site_url, body=body).execute() 

希望这会帮助别人。