1

有没有人让 TD Ameritrade 流媒体 API 与 C# 一起使用?他们在这里有一些文档和 JavaScript 示例https://developer.tdameritrade.com/content/streaming-data。我已经让 JavaScript 示例在https://js.do/上工作,但无法获得与在 .NET 中工作类似的东西。这是我正在尝试做的缩短版本。我无法准确包含我发送的内容,因为我正在尝试发送包含帐户信息的登录消息,但我可以说我将在我的 JavaScript 测试中工作的确切 JSON 消息复制并粘贴到文件 LoginJSON .txt 在此示例中。在此示例中,套接字将在我发送消息后立即关闭,根本没有文本响应。但是,如果我发送故意格式错误的消息,我实际上会收到说明消息格式错误的文本响应,然后断开套接字连接。他们的支持没有回应,我理解这是常态。这里有一些python示例https://www.youtube.com/channel/UCBsTB02yO0QGwtlfiv5m25Q,但我都看过了,还没有学到任何东西来帮助我让我的代码正常工作。

        ClientWebSocket socket = new ClientWebSocket();
        var connectAsync = socket.ConnectAsync(new Uri("wss://streamer-ws.tdameritrade.com/ws"), CancellationToken.None);
        string loginRequest;
        using (StreamReader re = new StreamReader("LoginJSON.txt")) {
            loginRequest = re.ReadToEnd();
        }

        connectAsync.Wait();

        Thread readThread = new Thread(
            delegate(object obj)
            {
                while (true) {
                    if (socket.State == WebSocketState.Open) {
                        Console.Out.WriteLine("Waiting");
                        byte[] recBytes = new byte[1024];
                        var clientBuffer = new ArraySegment<byte>(recBytes);
                        var receiveAsync = socket.ReceiveAsync(clientBuffer, CancellationToken.None);
                        receiveAsync.Wait();
                        switch (receiveAsync.Result.MessageType) {
                            case WebSocketMessageType.Text:
                                var s = Encoding.UTF8.GetString(recBytes);
                                Console.Out.WriteLine(s.Trim());
                                break;
                            case WebSocketMessageType.Close:
                                Console.Out.WriteLine("Close message received");
                                break;
                            default:
                                throw new ArgumentOutOfRangeException();
                        }
                    }
                }
            });

        readThread.Start();
        socket.SendAsync(Encoding.UTF8.GetBytes(loginRequest), WebSocketMessageType.Text, true, CancellationToken.None);
        Console.ReadLine();
4

5 回答 5

2

我遇到了同样的问题,我设法解决了。在我的情况下,时间戳没有正确准备,需要计算时间戳以获得 TokenTimestamp 属性,该属性应转换为通用时间。对不起,我的英语来自谷歌翻译。:) 这是正确的代码:

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime tokenDate = Convert.ToDateTime(userPrincipal.StreamerInfo.TokenTimestamp);
TimeSpan tokenEpoch = tokenDate.ToUniversalTime() - epoch;
long timestamp = (long)Math.Floor(tokenEpoch.TotalMilliseconds);

var credentials = new Credentials
{
    userid = userPrincipal.Accounts[0].AccountId,
    token = userPrincipal.StreamerInfo.Token,
    company = userPrincipal.Accounts[0].Company,
    segment = userPrincipal.Accounts[0].Segment,
    cddomain = userPrincipal.Accounts[0].AccountCdDomainId,
    usergroup = userPrincipal.StreamerInfo.UserGroup,
    accesslevel = userPrincipal.StreamerInfo.AccessLevel,
    authorized = "Y",
    timestamp = timestamp,
    appid = userPrincipal.StreamerInfo.AppId,
    acl = userPrincipal.StreamerInfo.Acl
};
var credentialArr = credentials.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Select(p => new KeyValuePair<string, string>(p.Name, p.GetValue(credentials, null).ToString()));
var loginRequest = new Request
{
    service = "ADMIN",
    command = "LOGIN",
    requestid = "0",
    account = userPrincipal.Accounts[0].AccountId,
    source = userPrincipal.StreamerInfo.AppId,
    parameters = new Parameters
    {
        credential = string.Join("&", credentialArr.Where(c => !string.IsNullOrWhiteSpace(c.Value)).Select(c => string.Format("{0}={1}", HttpUtility.UrlEncode(c.Key, Encoding.UTF8), HttpUtility.UrlEncode(c.Value, Encoding.UTF8)))),
        token = userPrincipal.StreamerInfo.Token,
        version = "1.0",
        qoslevel = "0"
    }
};
var req = JsonConvert.SerializeObject(Requests.ToRequests(loginRequest), Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
socketClient = new WebSocket(string.Format("wss://{0}/ws", userPrincipal.StreamerInfo.StreamerSocketUrl));
if(Environment.OSVersion.Version.Major > 5)
{
    socketClient.SslConfiguration.EnabledSslProtocols = (System.Security.Authentication.SslProtocols)3072;
    socketClient.SslConfiguration.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
}
socketClient.Connect();
socketClient.Send(req);
于 2020-06-23T12:59:44.870 回答
0

我尝试了 WebSocketClient 方法,但从未让它正常工作。我得到了完全相同的错误。确切地。我发现 WebSocketClient 实际上使在 javascript 中非常简单的实现变得复杂。只需让您的 C# 调用 javascript 函数来执行 javascript 并将响应发送回给您。我已经在 Blazor 应用程序中使用 C# 以这种方式工作,并且可以无缝工作。

于 2020-05-22T23:30:09.257 回答
0

@Mikhail,你能分享你获取用户主体的代码吗?这是我的代码,但即使我的访问令牌有效,我也会得到 status=401(我已经通过 API 页面对其进行了测试):

    using System;
    using WebSocketSharp;
    using System.Net.Http;
    using System.Threading.Tasks;

namespace TdLogin
{
    class Program
    {
        static async Task  Main(string[] args)
        {
            string accessToken = util.accessToken; // get the access token from util 
            Console.WriteLine("Hello World!");
            var client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", accessToken);
            var result = await client.GetAsync("https://api.tdamer`enter code here`itrade.com/v1/userprincipals?fields=streamerSubscriptionKeys%2CstreamerConnectionInfo");
            Console.WriteLine("status= {0}", result.StatusCode);
            Console.WriteLine(result.Content);
            Console.ReadKey();
        }
    }
}
于 2021-05-19T21:00:19.260 回答
0

好吧,TDA 报价总是在(纽约)东部时间,因为它是纽约证券交易所的标准。这就是为什么旧手表会显示不止一次的原因。

我还没有真正玩过流媒体,但经历了整个时代。我在东部时间,所以我不必处理转换。因此,以下转换未经过实战测试,但以下 C# 方法可能会帮助将来偶然发现此线程的人。

    private static readonly TimeZoneInfo TDAServerTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    public static DateTime ToServerTime(this DateTime dateTime)
    {
        return TimeZoneInfo.ConvertTime(dateTime, TDAServerTimeZone);
    }

    public static DateTime ToLocalTime(this DateTime dateTime)
    {
        TimeZoneInfo LocalTimeZone = TimeZoneInfo.Local;
        return TimeZoneInfo.ConvertTime(dateTime, TDAServerTimeZone, LocalTimeZone);
    }

    public static DateTime ToUTCTime(this DateTime dateTime)
    {
        TimeZoneInfo UTCTimeZone = TimeZoneInfo.Utc;
        return TimeZoneInfo.ConvertTime(dateTime, TDAServerTimeZone, UTCTimeZone);
    }

    public static DateTime FromUnixTime(long unixTime)
    {
        DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0);
        return epoch.AddMilliseconds(unixTime);
    }

    public static long ToUnixTime(DateTime dtConvert)
    {
        TimeSpan t = dtConvert - new DateTime(1970, 1, 1);
        long SinceEpoch = (long)t.TotalMilliseconds;
        return SinceEpoch;
    }
于 2022-01-22T11:02:15.850 回答
-1

这是 GitHub 上的 C# API。它们还支持其他语言:C#、Python、JavaScript、C++ 和 Ruby。 https://github.com/td-ameritrade

我不知道这是否来自 TD Ameritrade。

于 2021-08-20T19:14:05.273 回答