c# - jet.com 合作伙伴 API - Rest、JSON、RestSharp?

标签 c# json post restsharp

我有一个用 VB.net 编写的自定义订单管理系统。我现在需要使用 jet.com 合作伙伴 api 与他们集成。我相信这对你们这些 Web 专家来说并不难,但我对 RESTful 调用 API 没有太多经验。

documentation I have说:

Jet API Security Flow
The following security flow is the assumed flow for accessing Jet API functions that require authorization:

User submits his/her credentials over SSL/TLS to Jet API /api/Token endpoint via HTTPS POST request, like below:

POST https://merchant-api.jet.com/api/Token HTTP/1.1 
Host: merchant-api.jet.com 
Content-Type: application/json; charset=utf-8 
Content-Length: 42 

{"user":"dummy@jet.com","pass":"XXXXXXXX"} 

/api/Token endpoint is a single point of authentication for Jet API users. Therefore, it does not require either authentication or authorization.

If the user successfully authenticated, the security (bearer) token is returned back over SSL/TLS carrying claims associated with the user encoded and signed:

HTTP/1.1 200 OK 
Cache-Control: no-store 
Pragma: no-cache 
Content-Length: XXX 
Content-Type: application/json; charset=utf-8 
Server: Microsoft-IIS/8.0 
Set-Cookie: auth0=XXX...XX; path=/; secure; httponly 
Server: nginx/1.7.1 
X-AspNet-Version: 4.0.30319 
X-Powered-By: ASP.NET 
Date: Wed, 06 Aug 2014 13:50:17 GMT 
Set-Cookie: SERVERID=03-4BRURJ6FFLMCK; path=/ 

{"id_token":"XXX...XX","access_token":"XXX...XX","token_type":"bearer"} 

Token is contained in JSON node id_token that should be used in the following HTTP requests to Jet API as authorization vehicle (see article 4 below) until it expires or gets refreshed.

If not authenticated at the /api/Token endpoint, the user gets back HTTP response carrying header HTTP/1.1 401 Unauthorized. Having the above bearer token, the user then should provide it when calling other Jet API endpoints as part of HTTPS requests within Authorization header. This header has the following form: Authorization: Bearer If the user has proper permissions associated with the token he/she will be allowed to access the endpoints and/or specific data assigned by those permissions. The request will be executed and response will be defined by the correspondent called API function, otherwise HTTP/1.1 401 Unauthorized response is returned back.

Bearer tokens provided to Jet API users have a limited lifetime. However, until the token expires, the bearer token will be a full substitute for user credentials, so the token must be handled as private cryptographic material.

谁能告诉我一些示例代码以在 C# 中执行此操作?我当时认为使用 RestSharp 会有所帮助,但也没有任何这方面的经验。

对于他们的 API,我需要:

  1. 授权我的 API 调用
  2. 上传 JSON 格式的产品
  3. 检索订单信息
  4. 等等

因此,我停留在第一阶段,授权我的 API 调用。

最佳答案

您好,您可以使用此示例代码,我使用 this link 生成了此代码

var client = new RestClient("https://merchant-api.jet.com/api/token/");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "xxxx");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Basic xxxx");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"user\":\"xxxx\",\"pass\":\"xxxx\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

关于c# - jet.com 合作伙伴 API - Rest、JSON、RestSharp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33179985/

相关文章:

c# - 在 NHibernate.Linq 查询中出现 'OFFSET' 错误附近的错误语法

c# - 如何估计 WinForms 中方法的结束时间以正确通知用户预计完成时间?

java - 创建动态类型列表

javascript - 将字符串从 javascript 代码发布到服务器上的 ApiController

c# - !=-运算符对结构失败

c# - Action<Action> 是什么意思?

php - php中的json解析响应

php - 如何解析PHP中的elasticsearch响应

python - 具有多个模型和多个表单的 Django UpdateView 不起作用

ios - AFNetworking POST 无法返回 json 数据