Giter VIP home page Giter VIP logo

Comments (10)

bnoffer avatar bnoffer commented on June 27, 2024 1

Hi @wilbarclay ,

  1. check if your credentials are correct (client_id & client_secret)
  2. check if the first request provided you with the code (should be a GUID)

If the issue still persists try uninstalling and reinstalling the API Plugin. That solved an issue for me.

I attached my current ApiClient class for further reference.
ApiClient.cs.zip

The AuthorizeClient method works just fine on my install now, same goes for the GET method. The Post and Put are currently work in progress.

from api-plugin-for-nopcommerce.

lculjak avatar lculjak commented on June 27, 2024 1

I am using the code spinet with RestSharp. It is working perfect but is it possible that after sometime inactive I get /oauth/authorize response BadRequest 'Bad Request' ?

if I deploy the website, the error is fixed, but then is back.

from api-plugin-for-nopcommerce.

wilbarclay avatar wilbarclay commented on June 27, 2024

@poyker can you give any update on this? if it is a no that is fine, i will have to use another solution. Thank you

from api-plugin-for-nopcommerce.

bnoffer avatar bnoffer commented on June 27, 2024

Here is a code snippet using RestSharp that does the job:

string url = "https://www.yourstore.com";
string client_id = "client_id";
string client_secret = "client_secret";
//request token
var restclient = new RestClient(url);
RestRequest request = new RestRequest("/oauth/authorize") { Method = Method.GET };
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("client_id", client_id);
request.AddParameter("response_type", "code");
var tResponse = restclient.Execute(request);
var code = tResponse.ResponseUri.Query.Replace("?code=", "");

request = new RestRequest("/api/token") { Method = Method.POST };
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("client_id", client_id);
request.AddParameter("client_secret", client_secret);
request.AddParameter("code", code);
request.AddParameter("grant_type", "authorization_code");
tResponse = restclient.Execute(request);
var responseJson = tResponse.Content;
var token = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseJson)["access_token"].ToString();

In order for this to work you have to configure the CallBack URL to be your stores Url, in the example it is: https://www.yourstore.com

from api-plugin-for-nopcommerce.

wilbarclay avatar wilbarclay commented on June 27, 2024

thank you @bnoffer that is perfect.

I am having trouble with the second request though :(

nopapierror

from api-plugin-for-nopcommerce.

wilbarclay avatar wilbarclay commented on June 27, 2024

Yes the first request works and passes back the GUID code.

I will try reinstalling tomorrow, thank you.

from api-plugin-for-nopcommerce.

wilbarclay avatar wilbarclay commented on June 27, 2024

Hi @bnoffer

Reinstalling didn't fix it, passes back the code but getting the same error on the token request.

I am using nop 3.80

from api-plugin-for-nopcommerce.

bnoffer avatar bnoffer commented on June 27, 2024

I am using 3.80 as well.

The error that your screenshot displays normally happens if something went wrong handling the request on the Nop side. I had this happen at the first stage with the OAuth path, wich was not registered properly, which was fixed by the reinstall.

Please check your Admin area > System > Warnings and System > Log for more information on the error.

from api-plugin-for-nopcommerce.

darcyreimer avatar darcyreimer commented on June 27, 2024

In the ClientService class, include support for the ClientCredentials grant type (in the InsertClient() method):

new ClientGrantType() { Client = client, GrantType = OidcConstants.GrantTypes.ClientCredentials }

You can then use the method described at the bottom of the Protecting an API using Client Credentials document to access the API:

        // discover endpoints from metadata
        var disco = await DiscoveryClient.GetAsync("http://nopcommerceservername:portnumber");

        if (disco.IsError)
        {
            Console.WriteLine(disco.Error);
            return;
        }

        // request token
        var tokenClient = new TokenClient(disco.TokenEndpoint, "<<client ID>>", "<<client secret>>");
        var tokenResponse = await tokenClient.RequestClientCredentialsAsync("nop_api");

        if (tokenResponse.IsError)
        {
            Console.WriteLine(tokenResponse.Error);
            return;
        }

        Console.WriteLine(tokenResponse.Json);

        // call api
        var client = new HttpClient();
        client.SetBearerToken(tokenResponse.AccessToken);

        var response = await client.GetAsync("http://nopcommerceservername:portnumber/api/customers");
        if (!response.IsSuccessStatusCode)
        {
            Console.WriteLine(response.StatusCode);
        }
        else
        {
            var content = await response.Content.ReadAsStringAsync();
            Console.WriteLine(JArray.Parse(content));
        }

from api-plugin-for-nopcommerce.

divyang-desai avatar divyang-desai commented on June 27, 2024

@bnoffer, Any update for nopCommerce 4.0? as I add above code snippiest in 4.0, nopCommerce store stops running.

from api-plugin-for-nopcommerce.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.