Comments (10)
Hi @wilbarclay ,
- check if your credentials are correct (client_id & client_secret)
- 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.
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.
@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.
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.
thank you @bnoffer that is perfect.
I am having trouble with the second request though :(
from api-plugin-for-nopcommerce.
Yes the first request works and passes back the GUID code.
I will try reinstalling tomorrow, thank you.
from api-plugin-for-nopcommerce.
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.
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.
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.
@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)
- when we are expecting version 4.3? HOT 1
- When the 4.3 support will be ? Any date & time please HOT 2
- Enable email/password and token authentication
- No image details in product and category HOT 6
- getting 403 Forbidden HOT 5
- API for ProductSearch available?
- Some help for using Postman? HOT 1
- Product category
- What About splitting API Plugin in 2 ?
- API Plugin 4.20
- API List for API Plugin 4.2 HOT 1
- Add Shipments to the API HOT 1
- Missing NuGet Assembly on deployment
- ProductAttributeMappingDto - An item with the same key has already been added HOT 1
- Community Fork? HOT 1
- API for Nopcommerce 4.4 (.net 5) HOT 2
- Banner API
- TierPrices
- Forgot Password API
- Nop Translation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from api-plugin-for-nopcommerce.