[英]connecting to web api using c#
I'm trying to autheniicate using the code below to silverpop although I'm getting a status code of 400 when attempting. 我正在尝试使用下面的代码进行身份验证以进行Silverpop,尽管尝试时得到的状态代码为400。 Any suggestions as I'm not sure what else to try?!
有什么建议,因为我不确定还可以尝试什么? I can see the call going out using Fiddler but I've ran out of ideas.
我可以使用Fiddler看到电话打断,但我的想法已经用完了。 Many thanks
非常感谢
The server is returning the following error message: 服务器返回以下错误消息:
Code snippet 程式码片段
var httpWReq = (HttpWebRequest)WebRequest.Create("https://api5.silverpop.com/oauth/token");
var postData = string.Format("&grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);
// Also try this string but I get the same response
//var postData = string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);
var encoding = new ASCIIEncoding();
var data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (var stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)httpWReq.GetResponse();
var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
Response from server 来自服务器的响应
The remote server returned an error: (400) Bad Request.
This seems to work: 这似乎可行:
var httpWReq = (HttpWebRequest)WebRequest.Create("https://api5.silverpop.com/oauth/token" + string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", clientId, clientSecret, refreshToken));
var postData = "";
var encoding = new ASCIIEncoding();
var data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (var stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)httpWReq.GetResponse();
var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
try change change from 尝试从更改更改
var postData = string.Format("&grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);
to 至
var postData = string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);
This usually indicates that you are contacting the server in the wrong way. 这通常表示您以错误的方式联系服务器。 Are you perhaps missing a '?'
您可能错过了“?” in your post data?
在您的帖子数据中?
EDIT 编辑
Try: 尝试:
var client = new HttpClient();
var content = new FormUrlEncodedContent(<put content here>);
var response = await client.PostAsync("<your API URL>", content);
If you check the request with Fidler you'll see the parameters in body in both cases (with preceding & and with preceding ? signs). 如果您使用Fidler检查请求,则两种情况下都将在正文中看到参数(带有&和带有?符号)。
With & sign: 带有&符号:
With ? 用? sign:
标志:
The proper thing to do would be to remove the ? 正确的做法是删除? or the & signs in front and then the parameter name in body will be grant_type.
或前面的&符号,然后正文中的参数名称将为grant_type。 Code that works:
起作用的代码:
var postData = string.Format("grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}",
config.ClientId, config.ClientSecret, config.RefreshToken);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.