【问题标题】:Query Comcast/Xfinity monthly data quota via .NET/C#通过 .NET/C# 查询 Comcast/Xfinity 每月数据配额
【发布时间】:2019-07-26 13:27:55
【问题描述】:

以下代码报告“错误:未经身份验证”。

如何使用 .NET 对此 Comcast/Xfinity API 进行身份验证,以查询/获取具有每月配额的帐户的已用和可用数据?

static async Task Main() {
    using (var httpClient = new HttpClient()) {
        using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://customer.xfinity.com/apis/services/internet/usage")) {
            var response = await httpClient.SendAsync(request);
            var responseStream = await response.Content.ReadAsStreamAsync();
            var streamReader = new StreamReader(responseStream, Encoding.UTF8);
            var responseContent = streamReader.ReadToEnd(); // {"error":"unauthenticated"}
        }
    }
}

【问题讨论】:

    标签: c# oauth dotnet-httpclient quota undocumented-behavior


    【解决方案1】:

    想通了。此代码在 .NET Framework(测试 4.7.1)或 .NET Core(测试 2.2)中运行。它使用另外定义的UsernamePassword 值进行身份验证,并打印使用的数据和当月剩余的数据。

    static async Task Main() {
        using (var httpClient = new HttpClient()) {
            double totalUsage;
            double allowableUsage;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // this line can be removed in .NET Core
            using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://login.xfinity.com/login")) {
                var data = new Dictionary<string, string> {
                    {"user", Username}
                    , {"passwd", Password}
                    , {"s", "oauth"}
                    , {"continue", "https://oauth.xfinity.com/oauth/authorize?client_id=my-account-web&prompt=login&redirect_uri=https%3A%2F%2Fcustomer.xfinity.com%2Foauth%2Fcallback&response_type=code"}
                };
                var content = string.Join("&", data.Select(x => $"{x.Key}={WebUtility.UrlEncode(x.Value)}"));
                request.Content = new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded");
                await httpClient.SendAsync(request);
            }
            using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://customer.xfinity.com/apis/services/internet/usage")) {
                var response = await httpClient.SendAsync(request);
                var responseStream = await response.Content.ReadAsStreamAsync();
                var streamReader = new StreamReader(responseStream);
                var responseContent = streamReader.ReadToEnd();
                var parsedResponse = JObject.Parse(responseContent);
                var usageMonths = parsedResponse["usageMonths"];
                var currentMonthUsage = usageMonths.Last;
                totalUsage = currentMonthUsage.Value<double?>("totalUsage") ?? 0;
                allowableUsage = currentMonthUsage.Value<double?>("allowableUsage") ?? 0;
            }
            Console.WriteLine($"Allowable: {allowableUsage}");
            Console.WriteLine($"Total    : {totalUsage}");
            Console.ReadKey();
        }
    }
    

    取决于nuget.org/packages/Newtonsoft.Json/12.0.1

    【讨论】:

    • 我认为您的意思是 .NET Framework (4.7.1) 和 .NET Core (2.2) ;)
    • 正确!谢谢!我已经修好了。
    猜你喜欢
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 2021-11-03
    • 2018-04-16
    • 2020-10-30
    • 2018-08-23
    相关资源
    最近更新 更多