我对magento restsharp Rest API非常陌生。我刚设法得到一个管理员访问令牌。对我来说,下一步是接收te magento类别,但我并没有真正找到使用其余api命令的方法。我想我需要使用这个:GET /V1/categories
我只是不知道如何使用它。如果有人可以帮我解决这个问题,我将非常感激。
也许很高兴知道:
Visual Studio 2017
休息API
C#(restsharp)
的oauth2
magento 2
我不知道如何使用我在静态void结束时收到的管理令牌。
到目前为止我的代码:
static void Main(string[] args)
{
//Base URL needs to be Specified
String host = "http://beta.topprice24.com";
//Relative URL needs to be Specified
String endpoint = "/rest/default/V1/integration/admin/token";
RestClient _restClient = new RestClient(host);
var request = new RestRequest(endpoint, Method.POST);
//Initialize Credentials Property
var userRequest = new Credentials { username = "blabla", password = "blabla" };
var inputJson = JsonConvert.SerializeObject(userRequest);
//Request Header
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
//Request Body
request.AddParameter("application/json", inputJson, ParameterType.RequestBody);
var response = _restClient.Execute(request);
var token = response.Content;
}
public void getCategories()
{
String host = "http://beta.topprice24.com";
String endpoint = "/rest/V1/categories";
String token = "Bearer " + magento acces token?;
RestClient restClient = new RestClient(host);
var request = new RestRequest(endpoint, Method.GET);
request.AddHeader("Authorization", token);
var rawResponse = restClient.Execute(request);
var responseBody = rawResponse.Content;
}
答案 0 :(得分:0)
根据更新的问题代码更新了答案:
修改后的代码:
public String getCategories(String adminToken)
{
String host = "http://beta.topprice24.com";
String endpoint = "rest/V1/categories";
String token = "Bearer " +adminToken;
RestClient restClient = new RestClient(host);
var request = new RestRequest(endpoint, Method.GET);
request.AddHeader("Authorization", token);
var rawResponse = restClient.Execute(request);
return rawResponse.Content;
}
在您的主要功能中,您可以调用以下方法
修改后的主要功能:
request.AddParameter("application/json", inputJson, ParameterType.RequestBody);
var response = _restClient.Execute(request);
var token = response.Content;
var categoriesResult=getCategories(token);
Console.WriteLine("Categories List :" + categoriesResult);