【问题标题】:How to get magento admin token with restsharp如何使用 restsharp 获取 magento 管理员令牌
【发布时间】:2018-05-23 13:57:41
【问题描述】:

我对rest API 和restsharp 还很陌生,所以我需要一些帮助。我需要获得一个 magento 2.2.3 版管理令牌,但我一直收到一个错误的请求。我遵循了本教程:https://www.youtube.com/watch?v=2sdGuC7IUAI&t=343s。但我最终得到了一个错误的要求。当我使用教程中的断点检查状态码时,我得到:NotFound。

我的主要目标是获取我在 Magento 中拥有的类别。但要做到这一点,我需要一个管理员令牌。我已经有了不记名访问代码等。

非常感谢您的帮助。

到目前为止我的代码: magento.cs:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using RestSharp;  
using Newtonsoft.Json;

namespace MagentoTest
{
    public class magento
    {
        private RestClient Client { get; set; }
        private string Token { get; set; }

        public magento(string magentoUrl)
        {
            Client = new RestClient(magentoUrl);
        }

        public magento(string magentoUrl,string token)
        {
            Client = new RestClient(magentoUrl);
            Token = token;
        }

        public string GetAdminToken(string userName, string passWord)
        {
            var request = CreateRequest("/rest/V1/integration/admin/token", Method.POST);
            var user = new Credentials();
            user.username = userName;
            user.password = passWord;

            string Json = JsonConvert.SerializeObject(user, Formatting.Indented);

            request.AddParameter("aplication/json", Json, ParameterType.RequestBody);

            var response = Client.Execute(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return response.Content;
            }
            else
            {
                return "";
            }
        }

        private RestRequest CreateRequest(string endPoint, Method method)
        {
            var request = new RestRequest(endPoint, method);
            request.RequestFormat = DataFormat.Json;
            return request;
        }
    }
}

凭据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MagentoTest
{
    public class Credentials
    {
        public string username { get; set; }
        public string password { get; set; }
    }
}

(客户端) 程序.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MagentoTest;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            GetToken("blabla", "blabla");
        }

        static void GetToken(string userName, string passWord)
        {
            var m2 = new magento("http://beta.topprice24.com");
            string token = m2.GetAdminToken(userName, passWord);

        }
    }
}

【问题讨论】:

  • 您能分享一下您到目前为止创建的内容吗?
  • 我已经编辑了这个问题,因为我无法将代码放在评论中。而不是真正的用户名和密码,我当然放了blabla。

标签: c# api magento magento2 restsharp


【解决方案1】:

看起来,相对 URL 需要更改为“/rest/default/V1/integration/admin/token” (https://devdocs.magento.com/guides/v2.1/get-started/order-tutorial/order-admin-token.html)。

上面的代码我已经简化了,你可以很容易的拿到token。

保持您的 Credentials 类不变,并如下更改您的主程序

修改后的代码:(Program.cs)

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client
{
    class Program
    {
        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;         
        }
    }
}

【讨论】:

  • 你能检查你输入的 JSON 吗?
  • 我已经修改了你的主程序,更新后的代码可以正常工作。请检查更新的答案。
  • 根据文档 (devdocs.magento.com/guides/v2.1/get-started/order-tutorial/…),您可以通过传递管理员用户名和密码来获取管理员令牌。请重新检查以上更新的代码
  • 我收到了令牌。非常感谢!
  • 很高兴知道它有帮助:) 请接受答案并投赞成票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
相关资源
最近更新 更多