用于 Wrike API v3 的 Ajax/ASP.net Oauth2

Posted

技术标签:

【中文标题】用于 Wrike API v3 的 Ajax/ASP.net Oauth2【英文标题】:Ajax/ASP.net Oauth2 for Wrike API v3 【发布时间】:2016-09-16 20:14:33 【问题描述】:

我一直在尝试使用 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

更新:

我尝试使用 C# 通过 ASP.net 后端执行此操作,但仍然收到错误 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"];

    


【问题讨论】:

你为什么期待jsonp而不指定回调?我会建议摆脱 corssDomain 并将 dataType 更改为 'json' 我使用this post 作为参考。将 jsonp 更改为 json 并删除跨域的结果给了我这个错误:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'localhost:59614' 不允许访问。响应的 HTTP 状态代码为 400。 您遇到跨域问题的原因是您发布了错误的请求,并且您没有正确的响应标头。我怀疑你的帖子数据有问题。我建议您在将其放入 ajax 之前使用Postman 调查您的请求。 谢谢,我只是这样做了,我的回复是这样的: "error": "invalid_grant", "error_description": "Authorization code is invalid" 我从浏览器复制了授权码,所以这可能会导致该问题。我不认为我可以从邮递员那里得到它,因为它会将我带到一个登录页面。 【参考方案1】:

好的,经过大量时间和努力,我找到了解决方案。我决定通过后端 C# 获取令牌,而不是使用 ajax 获取访问令牌并处理跨域问题。您仍然需要为访问令牌解析令牌 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#

【讨论】:

以上是关于用于 Wrike API v3 的 Ajax/ASP.net Oauth2的主要内容,如果未能解决你的问题,请参考以下文章

用于任意页面的 Youtube Data API v3 pageToken

Google Maps v3 API密钥不适用于本地测试

Youtube Data API v3 PlaylistItems 更新不适用于“稍后观看”播放列表

Python - youtube.playlistItems().delete() 不适用于 [已删除的视频] (YouTube API v3)

使用模板变量时,Django google-maps API v3 不适用于 Chrome

map.fitbounds google maps api v3 不适用于从 jQM 面板选择的位置