我一直在尝试使用Wrike API,但是我无法使用Ajax或ASP.Net获取访问令牌。我目前正在使用"Wrike for Developers Documentation",这是我第一次使用Oauth2。没有任何JavaScript或ASP.net示例,我只能在GitHub上找到node.js和PHP的项目。
以下是我用来尝试获取访问令牌的代码:
$.ajax({
type: 'POST',
url: "https://www.wrike.com/oauth2/token",
data: {
client_id: <client_id>,//I took these details out for the post
client_secret: <client_secret>,//I took these details out for the post
grant_type: "authorization_code",
code: get("code")//from redirect URI
},
crossDomain: true,
dataType: 'jsonp',
success: function (response) {
alert(response); // server response
}
});
但是我一直收到错误400(错误的请求)。我有什么明显的遗失吗?或者,如果不使用C#进行身份验证服务器端,这是不可能的?
我希望这对你来说足够了。提前谢谢。
我还有一些我认为通过这篇文章解决过的原始问题:Cross Origin Stack Over Flow在此之前我用它作为我的代码:
$.ajax({
type: 'Post',
url: "https://www.wrike.com/oauth2/token",
data: {
client_id: "<client_id>",
client_secret: <client_Secret>,
grant_type: "authorization_code",
code: get("code")
},
success: function (response) {
alert(response); // server response
}
});
我也试过这个来解决我的CORS问题,但无济于事:CORS on ASP.NET
更新
我尝试通过ASP.net后端使用C#我仍然收到错误400错误请求但是当我接受我用它做的请求并将其放入邮递员时我能够获得令牌。这是我正在使用的代码。如果我完全修复它,我会更新这个问题。
protected void Page_Load(object sender, EventArgs e)
{
Response.AppendHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
string ClientID = <ClientID>;
string ClientSecret = <ClientSecret>;
string code = Request.QueryString["code"];
string.Format("https://www.wrike.com/oauth2/token?client_id={0}&client_secret={1}&grant_type=authorization_code&code={2}", ClientID, ClientSecret, code);
if (Request["code"] == null)
{
Response.Redirect(string.Format(
"https://www.wrike.com/oauth2/authorize?client_id={0}&response_type=code",
ClientID));
}
else
{
Dictionary<string, string> tokens = new Dictionary<string, string>();
string url = string.Format("www.wrike.com/oauth2/token?client_id={0}&client_secret={1}&grant_type=authorization_code&code={2}", ClientID, ClientSecret, code);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string vals = reader.ReadToEnd();
foreach (string token in vals.Split('&'))
{
tokens.Add(token.Substring(0, token.IndexOf("=")),
token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
string access_token = tokens["access_token"];
}
}
答案 0 :(得分:1)
好的家伙经过大量的时间和努力后,我找到了解决方案。我决定通过后端C#获取令牌,而不是使用ajax获取访问令牌并处理Cross Origin问题。您仍然需要为访问令牌解析令牌var,但这很容易。作为参考,var标记包含以下信息(Wrike API Oauth2 documentation):
{
"access_token": "2YotnFZFEjr1zCsicMWpAA",
"refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
"token_type": "bearer",
"expires_in": 3600
}
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
string ClientID = "<clientID>";
string ClientSecret = "<ClientSecret>";
string code = Request.QueryString["code"];
string.Format("https://www.wrike.com/oauth2/token?client_id={0}&client_secret={1}&grant_type=authorization_code&code={2}", ClientID, ClientSecret, code);
if (Request["code"] == null)
{
Response.Redirect(string.Format(
"https://www.wrike.com/oauth2/authorize?client_id={0}&response_type=code",
ClientID));
}
else
{
string url = string.Format("https://www.wrike.com/oauth2/token?client_id={0}&client_secret={1}&grant_type=authorization_code&code={2}", ClientID, ClientSecret, code);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Headers.Add("Access-Control-Allow-Origin", "*");
request.Method = "Post";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
var token = reader.ReadToEnd();
}
}
}
本教程帮助我使用ASP.Net创建http请求: Post on Facebook User's wall using ASP.Net C#