Search code examples
c#magentomagento2restsharpmagento-rest-api

How to get magento admin token with restsharp


I'm pretty new to rest API and restsharp so I need some help. I need to get a magento version 2.2.3 admin token but I keep getting a bad request. I've followed this tutorial: https://www.youtube.com/watch?v=2sdGuC7IUAI&t=343s. But I'm ending up with a bad request. When I check the statuscode using a the breakpoints from the tutorial I get: NotFound.

My main goal is to get the categories I have in Magento. But to get that I need an admin token. I already have a bearer acces code etc.

I would really appreciate your help.

my code so far: 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;
        }
    }
}

Credentials:

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; }
    }
}

(Client) Program.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);

        }
    }
}

Solution

  • It looks, relative URL needs to be changed as "/rest/default/V1/integration/admin/token" (https://devdocs.magento.com/guides/v2.1/get-started/order-tutorial/order-admin-token.html).

    I have simplified the above code and you can easily get the token.

    Keep your Credentials class as it is and change your main program as below

    Modified Code:(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;         
            }
        }
    }