Binance API和angular 4 httpClient [英] Binance API and angular 4 httpClient

查看:75
本文介绍了Binance API和angular 4 httpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对加密市场币安有疑问. 他们有公共api,不过我可以用它来创建交易应用程序.

I have a question about cryptomarket Binance. They have public api which I though I could use in angular to create trading app.

但是我有一些麻烦. 在chrome中使用该链接,我得到json结果. https://api.binance.com/api/v1/exchangeInfo

But I have some troubles. Using that link in chrome I get json result. https://api.binance.com/api/v1/exchangeInfo

但与angular 4 httpClient一起使用:

But using with angular 4 httpClient:

this.http.get('https://api.binance.com/api/v1/exchangeInfo').subscribe(res => console.log(res));

我有错误:跨域请求被阻止:同源策略禁止读取api.binance.com/api/v1/exchangeInfo上的远程资源. (原因:CORS标头"Access-Control-Allow-Origin"缺失)

I have error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at api.binance.com/api/v1/exchangeInfo. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)

它不起作用.我不明白,为什么我不能在角度应用程序中使用该API?

It doesn't work. I don't get it, why I can't use that API in angular app?https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md

我该怎么办? 我应该这样设置标题吗?

What should I do? Should I set headers like that:

getMarkets() {
    const headers = new HttpHeaders();
    headers.set('Content-Type', 'application/json');
    headers.set('Accept', 'application/json');
    headers.set('Access-Control-Allow-Headers', 'Content-Type');
    headers.set('Access-Control-Allow-Origin', '*');

    const path = 'https://api.binance.com/api/v1/exchangeInfo';
    return this.http.get(path, {headers: headers});
}

预先感谢

推荐答案

您不能像那样直接使用它,Binance API不会设置CORS标头,因此Chrome和所有主要的浏览器都将阻止请求. 还有更多功能,但是本质上,需要启用对CORS的支持的api服务器应该将Access-Control-Allow-Origin设置为*或将单个域设置为www.example.com,这允许浏览器阻止站点上的恶意代码.调用并读取您可能已登录到其他站点的某些数据的响应(例如:银行信息) 您可以在此处

You can't quite use it directly like that, Binance API does not set CORS headers, so Chrome and all major browsers will block the requests. There is a bit more to it, but essentially, the api servers that need to enable support for CORS should set Access-Control-Allow-Origin to be * or a single domain www.example.com, this allows the browsers to prevent malicious code on a site to call and read the response of some data from other site you might be logged on to ( eg: bank info ) You can read more about it here

一种可能的解决方案是拥有自己的服务器来代理对币安的呼叫

One possible solution is to have your own server that proxies calls to binance

如果要进行测试,另一种解决方案是使用启用CORS的扩展程序,例如

Another solution if you're testing things out is to use a CORS enabling extension like this one

更新:如果满足您的数据需求,您也可以使用websocket API

Update: You can also use the websocket API if that satisfies your data needs docs

更新2:这是一个很好的stackoverflow 问题在cors上

Update 2: Here's a good stackoverflow question on cors

旁注:如果银行的API服务器将Access-Control-Allow-Origin设置为*,则更改银行:)

Side note: If your bank's API server sets the Access-Control-Allow-Origin to * then change banks :)

这篇关于Binance API和angular 4 httpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆