使用基本身份验证而不是API密钥的JSON

本文关键字:API 密钥 JSON 身份验证 | 更新日期: 2023-09-27 18:06:25

我必须从REST服务器下载数据并在SQL中填充表。因此,我使用一个使用API密钥的测试REST服务器站点来实现这一点。下面的代码。

var url="https://openexchangerates.org/api/latest.json?app_id=a28cb70b31404429bea46b0889ec6697";

      var currencyRates = CCAP_JSON_Downloader.CCAP._download_serialized_json_data<CurrencyRates>(url);
       // Console.Write();

public class CCAP
    {
        public static T _download_serialized_json_data<T>(string url) where T : new() {
  using (var w = new WebClient()) {
    var json_data = string.Empty;
    SqlConnection DB_CONN_1990 = new SqlConnection(ConfigurationManager.ConnectionStrings["CCAP_JSON_Downloader.Properties.Settings.DB_CONN_1990"].ToString());
        DB_CONN_1990.Open();
    // attempt to download JSON data as a string
        //for (int i = 0; i <= 9; i++)
            try
            {
                json_data = w.DownloadString(url);
                JsonTextReader reader = new JsonTextReader(new StringReader(json_data));
                while (reader.Read())
                {
                     Console.Read();
                    if (reader.Value != null)
                    {
                        Console.WriteLine("CAPTURING" + "Path: {0}, Value: {1}", reader.Path, reader.Value);
                        var cmd = new SqlCommand("Insert_RAW_XML", DB_CONN_1990);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Token_Type", reader.Path);
                        cmd.Parameters.AddWithValue("@Value", reader.Value);
                        cmd.ExecuteNonQuery();
                    }
                    else
                        Console.WriteLine("Token: {0}", reader.TokenType);

现在,我可以访问状态REST服务器,但它们使用BASICHTTP身份验证。以下的代码

var webRequest = (HttpWebRequest)WebRequest.Create("https://wccarest.wicourts.gov:443/api/v1/counties?expand=all&api_key");
            webRequest.Method = "GET";
            webRequest.ContentType = "application/json";
            webRequest.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:28.0) Gecko/20100101 Firefox/28.0";
            string autorization = "username" + ":" + "Password";
            byte[] binaryAuthorization = System.Text.Encoding.UTF8.GetBytes(autorization);
            autorization = Convert.ToBase64String(binaryAuthorization);
            autorization = "Basic " + autorization;
            webRequest.Headers.Add("AUTHORIZATION", autorization);
            var webResponse = (HttpWebResponse)webRequest.GetResponse();
            if (webResponse.StatusCode != HttpStatusCode.OK) Console.WriteLine("{0}", webResponse.Headers);
            using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
            {
             string s = reader.ReadToEnd();
          Console.WriteLine(s);
            }

我的问题是,我确实喜欢以前的工作方式。在那里我可以直接输入URL,var URL="https://openexchangerates.org/api/latest.json?app_id=a28cb70b31404429bea46b0889ec6697";

但现在,我不知道如何通过身份验证获得完整的URL。我有来自用户和密码的加密。但我似乎只能通过基本URL,所以我总是收到未经授权的错误。我如何提交URL和我可以传递到类CCAP的完整凭据。第一次使用REST服务器等,所以我愿意接受任何其他建议或更好的方法。谢谢Rudy

使用基本身份验证而不是API密钥的JSON

尝试使用特殊的URL格式:http://username:password@example.com/-这应该在标准HTTP"Authorization"标头中发送凭据。

例如,https://username:password@wccarest.wicorts.gov:443/api/v1/县?expand=all&api_key