所以基本上,我正在尝试在线连接到 REST API。够容易吧?
但是,每当我尝试连接时,我都会收到 401(未经授权)错误。我正在使用 C# 控制台应用程序对此进行测试,并且我也尝试过使用 PUTMAN(Google Chrome 应用程序来查看 HTTP 请求)。
这是我正在使用的 API 的链接:https ://community.dynatrace.com/community/display/APMSAASDOC/Login+-+REST+API
我遵循列出的所有步骤。我知道我的用户名和密码正确(已登录 Dynatrace 门户)。有谁知道可能出了什么问题?下面是我的代码(出于显而易见的原因,我删除了实际的用户名和密码):
static async Task RunAsync()
{
string _user;
string _password;
string _authorizationType;
string _contentType;
string _CredentialsToBase64;
string _url = "https://datafeed-api.dynatrace.com";
_user = "MYUSERNAME";
_password = "MYPASSWORD";
_authorizationType = "basic";
_contentType = "application/json";
_CredentialsToBase64 = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(_user + ":" + _password));
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_contentType));
client.DefaultRequestHeaders.Add("Authorization", _authorizationType + " " + _CredentialsToBase64);
using (HttpResponseMessage httpResponse = await client.GetAsync("publicapi/rest/v1.0/login?user=MYUSERNAME&password=MYPASSWORD HTTP/1.1"))
{
if (httpResponse.IsSuccessStatusCode)
{
Console.WriteLine("Success");
}
else
{
Console.WriteLine(string.Format("Service request failed ({0})", httpResponse.StatusCode));
}
}
}