我在工作中做侧面项目。我有非常基本的HTTP调用知识,如果我有noob问题,请原谅我。
我使用Postman(https://www.getpostman.com)来创建JSON响应序列化以及调用标头和参数。我正在尝试登录,然后调用方法getSIM
。当我通过Postman应用程序发送请求时,呼叫成功。当我使用我的C#应用程序尝试通话时,只有loginRequestAPICall
会返回响应("确定")但getSIM
通话失败('权限被拒绝') 。
此API的文档可在此处获得: https://www.eseye.com/wp-content/uploads/8281-Tigrillo-API-User-Guide.pdf
我假设问题是带有令牌的问题,但我没有足够的知识来解决这个问题。
这是我的代码:
class Program
{
static void Main(string[] args)
{
var client = new RestClient("https://siam.eseye.com/Japi/Tigrillo");
if (loginRequestAPICall(client))
{
Console.WriteLine("Login successful");
getSIM(client);
}
else
{
Console.WriteLine("Login unsuccessful");
}
Console.ReadLine();
}
public static bool loginRequestAPICall(RestClient client)
{
var request = new RestRequest("/login/", Method.POST);
//loginRequest.AddHeader("postman-token", "4e67ed4c-4130-4067-9539-d5ed6b6ad761");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\r\n\"username\" : \"XXXXXXXXX\" ,\r\n\"password\" : \"XXXXXXXXX\" ,\r\n\"portfolioID\" : \"XXXXXXXXXX\"\r\n}", ParameterType.RequestBody);
IRestResponse<LoginStatus> response = client.Execute<LoginStatus>(request);
Console.WriteLine(response.Data.status.status);
if (response.Data.status.status == "OK")
{
return true;
}
else
return false;
}
public static void getSIM(RestClient client)
{
var request = new RestRequest("/getSIMs/", Method.POST);
//request.AddHeader("postman-token", "4e67ed4c-4130-4067-9539-d5ed6b6ad761");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\r\n\"numRecs\" : 1\r\n}", ParameterType.RequestBody);
IRestResponse<RootObject> response = client.Execute<RootObject>(request);
Console.WriteLine("Found {0} devices", response.Data.totRecs);
}
}
public class LoginStatus
{
public Status status { get; set; }
public string cookie { get; set; }
public string permissions { get; set; }
public string canActivate { get; set; }
}
public class Status
{
public string errorCode { get; set; }
public string errorMessage { get; set; }
public string status { get; set; }
}
public class Sim
{
public string MSISDN { get; set; }
public string friendlyName { get; set; }
public string MEID { get; set; }
public string ICCID { get; set; }
public string IMSI { get; set; }
public int DataUsage { get; set; }
public string ipAddress { get; set; }
public string group { get; set; }
public string status { get; set; }
public string alert { get; set; }
public string controls { get; set; }
public string dataMSISDN { get; set; }
public string localMSISDN { get; set; }
public string mappedMSISDN { get; set; }
}
public class RootObject
{
public int totRecs { get; set; }
public List<Sim> sims { get; set; }
public Status status { get; set; }
}
}