[英]Getting 'Access denied' when calling REST service
I'm building a small app that consumes a REST service. 我正在构建一个使用REST服务的小型应用程序。
The REST service expects that the URL i interact with always have the API key as an URL parameter. REST服务期望与我交互的URL始终将API密钥作为URL参数。
So no matter if i GET, POST, UPDATE or DELETE, my URL should always contain be something like this: 因此,无论我是GET,POST,UPDATE还是DELETE,我的URL都应始终包含以下内容:
https://rest.service.tld:443/list?api_key=MY-KEY
https://rest.service.tld:443/lists/1/profiles/search?api_key=MY-KEY
I tried with the sample code from RestSharp webpage, but it get the statuscode Access Denied
我尝试过RestSharp网页上的示例代码,但它得到的状态码
Access Denied
Here's my code: 这是我的代码:
// Create client
var client = new RestClient( "https://rest.service.tld:443" );
client.Authenticator = new SimpleAuthenticator( "api_key", "MY-KEY", "", "" );
// GET list of items
var requestLists = new RestRequest( "lists", Method.GET );
IRestResponse<List<ListResponse>> listResponse = client.Execute<List<ListResponse>>( requestLists ); // Returns the correct list
// POST search
var requestProfiles = new RestRequest( "lists/1/profiles/search", Method.POST );
requestProfiles.AddParameter( "Criteria", "{\"email\":\my@email.tld\"}" );
IRestResponse profileResponse = client.Execute( requestProfiles ); // Returns 'Access Denied' status code
As far as i can tell, the POST
method doesn't contain the correct querystring, instead my api_key
is added as a parameter in the POST
. 据我所知,
POST
方法不包含正确的查询字符串,而是将我的api_key
作为参数添加到POST
。
Is there a way to keep my API_KEY
in the Querystring like i need it to be? 有没有办法像我需要的那样将我的
API_KEY
保留在API_KEY
中?
By default the api_key is added as a normal parameter, you need to explicitly enforce that you want the parameter to be embedded into the URL, by setting the ParameterType as follows: 默认情况下,将api_key添加为普通参数,您需要通过如下设置ParameterType来明确要求将参数嵌入到URL中:
var requestProfiles = new RestRequest( "lists/1/profiles/search{api_key}", Method.POST );
requestProfiles.AddParameter( "Criteria", "{\"email\":\my@email.tld\"}" );
requestProfiles.AddParameter( "api_key", MY-KEY, ParameterType.UrlSegment);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.