C# 来自ASP.Net网页C的销售人员身份验证#
我正在使用OAuth 2.0向销售人员验证我的网站。我下载并运行了Java示例代码,效果很好。现在我需要在ASP.Net网页上执行同样的操作,但它似乎不起作用。当我最后一次发布获取访问令牌时,它给了我一个错误System.Net.WebException:远程服务器返回了一个错误:(400)错误请求 我使用与此链接中提到的相同的httppost方法。有关详细信息,请参阅下面的代码C# 来自ASP.Net网页C的销售人员身份验证#,c#,asp.net,oauth,salesforce,C#,Asp.net,Oauth,Salesforce,我正在使用OAuth 2.0向销售人员验证我的网站。我下载并运行了Java示例代码,效果很好。现在我需要在ASP.Net网页上执行同样的操作,但它似乎不起作用。当我最后一次发布获取访问令牌时,它给了我一个错误System.Net.WebException:远程服务器返回了一个错误:(400)错误请求 我使用与此链接中提到的相同的httppost方法。有关详细信息,请参阅下面的代码 public partial class Authenticate2 : System.Web.UI.Page {
public partial class Authenticate2 : System.Web.UI.Page
{
private string tokenUrl;
protected void Page_Load(object sender, EventArgs e)
{
Logger.LogInfoMessage("I am Authenticate 2");
string code = Request["code"];
Logger.LogInfoMessage("Code is: " + code);
string parameters = "code=" + code
+ "&grant_type=authorization_code"
+ "&client_id=" + Authenticate.CONSUMER_KEY
+ "&client_secret=" + Authenticate.CONSUMER_SECRET
+ "&redirect_uri" + Authenticate.REDIRECT_URL;
string result = HttpPost(tokenUrl, parameters);
Logger.LogInfoMessage(result);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
tokenUrl = Authenticate.ENVIRONMENT + "/services/oauth2/token";
}
public string HttpPost(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
// Add parameters to post
byte[] data = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = data.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(data, 0, data.Length);
os.Close();
// Do the post and get the response.
System.Net.WebResponse resp = req.GetResponse();
if (resp == null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
}
authenticate类只是按照以下步骤执行重定向:
public partial class Authenticate : System.Web.UI.Page
{
public const string CONSUMER_KEY = "XXX";
public const string CONSUMER_SECRET = "XXX";
public const string REDIRECT_URL = "https://localhost/authenticate2.aspx";
public const string ENVIRONMENT = "https://test.salesforce.com";
public const string ACCESS_TOKEN = "ACCESS_TOKEN";
public const string INSTANCE_URL = "INSTANCE_URL";
public const string INVOICE_ID = "invoiceId";
private string authenticateUrl = null;
protected void Page_Load(object sender, EventArgs e)
{
Logger.LogInfoMessage("I am in Authenticate");
string accessToken = (string)Session[ACCESS_TOKEN];
Logger.LogInfoMessage("Access Token is:" + accessToken);
if (accessToken == null)
{
Logger.LogInfoMessage("Redirecting to:" + authenticateUrl);
Response.Redirect(authenticateUrl);
return;
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
authenticateUrl = ENVIRONMENT
+ "/services/oauth2/authorize?response_type=code&client_id="
+ CONSUMER_KEY + "&redirect_uri="
+ HttpUtility.UrlEncode(REDIRECT_URL, System.Text.Encoding.UTF8);
}
}
我得到的错误屏幕截图如下所示:
构建参数字符串时,需要对每个参数值进行URL编码。您还可以捕获WebException并读取响应正文,其中还将包含有关该问题的更多信息。您的HTTPPost参数构造不正确,这就是请求被返回为“错误请求”的原因:
此处缺少“=”+
“&redirect\u uri
=”+Authenticate.redirect\u URL代码>Salesforce API的oAuth实现有javascript版本,但您可以使用它来了解身份验证流程:
视频描述了获取令牌并使用令牌获取数据所需的逐步过程。您的参数字符串包含什么,以及当您获取400状态代码时,响应主体会说什么(应该有关于失败原因的更多详细信息)。如果此视频出现故障,此答案将无效。请试着在你的回答中包含一些信息,以解决原来的问题。
string parameters = "code=" + code
+ "&grant_type=authorization_code"
+ "&client_id=" + Authenticate.CONSUMER_KEY
+ "&client_secret=" + Authenticate.CONSUMER_SECRET
+ "&redirect_uri" + Authenticate.REDIRECT_URL;