I've been trying to work with the Wrike API, but I am having trouble getting my access token using Ajax or ASP.Net. I am currently using the "Wrike for Developers Documentation", and this is my first time working with Oauth2. There aren't any JavaScript or ASP.net examples and I could only find projects on GitHub for node.js and PHP.
Here is the code I am using to try and get the access token:
$.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
}
});
However I keep getting Error 400 (bad request). Is there anything obvious that I'm missing? Or is this not possible to do without doing the authentication server side using C#?
I hope this is enough information for you. thanks in advance.
I also had cross origin issues that I think I solved with this post: Cross Origin Stack Over Flow Prior to that I used this as my code:
$.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
}
});
I also tried this to fix my CORS issue but with no avail: CORS on ASP.NET
Update:
I tried doing it via the ASP.net back end with C# I still get a error 400 bad request but when I take the request I made with it and put it into postman I am able to get the token. Here is the code I'm using. I'll update this question if I fix it completely.
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"];
}
}