我正在尝试使用Matlab和可用的urlread2函数here访问betfair API。
编辑:如果有人可以提供帮助,我已经在Freelancer上发布了这个问题:tinyurl ... / pa7sblb 我关注的betfair API的文档是getting started guide。我已成功登录并使用以下代码保持会话打开:(我收到成功回复)
%% Login and get Token
url = 'https://identitysso.betfair.com/api/login';
params = {'username' '******' 'password' '******'};
header1 = http_createHeader('X-Application','*****');
header2 = http_createHeader('Accept','application/json');
header = [header1, header2];
[paramString] = http_paramsToString(params)
[login,extras] = urlread2(url,'POST',paramString,header)
login = loadjson(login)
token = login.token
%% Keep Alive
disp('Keep Session Alive')
url_alive = 'https://identitysso.betfair.com/api/keepAlive';
header1 = http_createHeader('X-Application','******');
header2 = http_createHeader('Accept','application/json');
header3 = http_createHeader('X-Authentication',token');
header_alive = [header1, header2, header3];
[keep_alive,extras] = urlread2(url_alive,'POST',[],header_alive);
keep_alive = loadjson(keep_alive);
keep_alive_status = keep_alive.status
当我尝试进行下一步并加载所有可用市场时,我的麻烦就开始了。我试图复制这个为Python设计的示例代码
import requests
import json
endpoint = "https://api.betfair.com/exchange/betting/rest/v1.0/"
header = { 'X-Application' : 'APP_KEY_HERE', 'X-Authentication' : 'SESSION_TOKEN_HERE' ,'content-type' : 'application/json' }
json_req='{"filter":{ }}'
url = endpoint + "listEventTypes/"
response = requests.post(url, data=json_req, headers=header)
我在Matlab使用的代码如下。
%% Get Markets
url = 'https://api.betfair.com/exchange/betting/rest/v1.0/listEventTypes/';
header_application = http_createHeader('X-Application','******');
header_authentication = http_createHeader('X-Authentication',token');
header_content = http_createHeader('content_type','application/json');
header_list = [header_application, header_authentication, header_content];
json_body = savejson('','filter: {}');
[list,extras] = urlread2(url_list,'POST',json_body,header_list)
我遇到了一个http响应代码415的问题。我认为服务器无法理解我的参数,因为我以前成功使用的标题。
非常感谢任何帮助或建议!
这是错误: 响应流未定义 下面是Java错误转储(截断): 使用urlread2时出错(第217行) 发生Java异常: java.io.IOException:服务器返回HTTP响应代码:415为URL ....
答案 0 :(得分:1)
我看了你的问题,似乎是由两件事引起的:
1)内容类型应表示为“content-type”而不是“content_type”
2)savejson-function不会创建足够的json-string。如果您使用Python脚本中的json-request,它就可以工作。
此代码适用于我:
%% Get Markets
url = 'https://api.betfair.com/exchange/betting/rest/v1.0/listEventTypes/';
header_application = http_createHeader('X-Application','*********');
header_authentication = http_createHeader('X-Authentication',token');
header_content = http_createHeader('content-type','application/json');
header_list = [header_application, header_authentication, header_content];
json_body = '{"filter":{ }}';
[list,extras] = urlread2(url,'POST',json_body,header_list)