2016-03-12 77 views
0

所以,我玩弄(从零售商店包含提供丹麦目录)Etilbudsavis' API(与使用Python的API播放)。 我的目标是获取基于搜索查询数据。 the API acutally allows this,开箱即用。但是,当我尝试这样做时,最终出现错误,说我的令牌丢失了。不管怎么说,这里是我的代码:为什么我会遇到这个错误 - “缺少令牌”?

from urllib2 import urlopen 
from json import load 
import requests 

body = {'api_key': 'secret_api_key'} 

response = requests.post('https://api.etilbudsavis.dk/v2/sessions', data=body) 

print response.text 

new_body = {'_token:': 'token_obtained_from_POST_method', 'query:': 'coca-cola'} 

new_response = requests.get('https://api.etilbudsavis.dk/v2/offers/search', data=new_body) 

print new_response.text 

完整的错误:

{"code":1107,"id":"00ilpgq7etum2ksrh4nr6y1jlu5ng8cj","message":"missing token"," 
details":"Missing token\nNo token found in request to an endpoint that requires 
a valid token.","previous":null,"@note.1":"Hey! It looks like you found an error 
. If you have any questions about this error, feel free to contact support with 
the above error id."} 

回答

1

由于这是一个GET请求,你应该使用params参数传递在URL中的数据。

new_response = requests.get('https://api.etilbudsavis.dk/v2/offers/search', params=new_body) 

参见the requests docs

+0

现在,我收到一个新的错误:“{”code“:1106,”id“:”00ilpnqbe2jeoxcnyejlh75vaho2c3jq“,”message“:”No Origin“,”deta ils“:”Missing origin header \ nThis token “:空,” @注1“:”不能没有有效的产地^ h EADER“”以前的使用。嘿,它看起来就像你发现了一个错误如果你 有这个错误的任何问题,请随时联系支持与ABOV E错误ID。“}' – DBE7

+1

看起来你需要签署令牌之前,你可以使用它,请参阅[他们的文档(http://docs.api.etilbudsavis.dk/docs/getting -started#API会话的签名)。 –

+0

它说:“如果你的eTilbudsavis应用程序是由JavaScript在浏览器中不执行,您需要登录您的令牌,然后才能开始发送请求不要简单地增加一个Origin标!”。我不写手机应用程序或类似的东西。我应该没有签约... – DBE7

0

我设法解决与丹尼尔·罗斯曼谁提醒我的事实,与在Python Shell中的API是打从浏览器的API交互不同的帮助的问题。该文档明确指出,您必须签署API令牌才能正常工作。我错过了那些微小的细节......丹尼尔帮我把所有东西都弄清楚了。再次感谢丹。

相关问题